Talk about the pits that have been trampled during the use of Echarts

Source: Internet
Author: User
Tags object object tojson

Summary:

1. Use jquery to get problems with JSON objects

Because of their use of Ajax is not skilled, before the code is copied others to use, this time I wrote it was a lot of trouble to list as follows:

1.1 There is also a point number between $ and Ajax, which is $.ajax ({...});

1.2 In the return value where the return type is text or JSON format, do not know which uses eval, which use (. property) to parse, the correct way is as follows

When the return type is text, use eval parsing to write as follows

var obj = eval ("(" + Data + ")"); var title = Obj.title;

When the return type is in JSON format, use the dot number directly:

var title = Data.title;

2. Problems encountered in converting objects to JSON format in Java programs

2.1 When you use the following code to convert an object to JSON format, an empty property value appears.

Jsonobject json = jsonobject.fromobject (option);

2.2 Use the following code to convert an object to a JSON-formatted property value for space-time is not displayed. But even that would produce a sequence of problems in 3.

String json = json.tojsonstring (option);

3. Order problems when converting objects to JSON strings in Java

Using the 2.2 method, although the empty property values do not appear when converted, the sort order is not output in the order in which the objects are loaded. This feeling is rather strange.

Online query, because JSON is used HashMap container, so is unordered. So find someone else's class solution on the Web to rewrite the JSON tostring method.

Paste as follows, for reference.

     Public StaticString Format (Object object) {Gson Gson=NewGsonbuilder (). setprettyprinting (). disablehtmlescaping (). Create (); Jsonparser JP=NewJsonparser (); Jsonelement JE=Jp.parse (Gson.tojson (object)); String prettyjsonstring=Gson.tojson (JE); //Simple processing functionString[] lines = prettyjsonstring.split ("\ n"); Lines=replacefunctionquote (lines); StringBuilder StringBuilder=NewStringBuilder ();  for(String line:lines) {stringbuilder.append (line); }        returnstringbuilder.tostring (); }
    /*** processing function and (function () {}) () in a string, except for the code in {}, where spaces are not allowed * *@paramlines *@return     */     Public Staticstring[] Replacefunctionquote (string[] lines) {Booleanfunction =false; Booleanimmediately =false;  for(inti = 0; i < lines.length; i++) {String line=Lines[i].trim (); if(!function && line.contains ("\" function ")) ) {function=true; Line= Line.replaceall ("\" function "," function ")); }            if(Function && line.contains ("}\" ") ) {function=false; Line= Line.replaceall ("\\}\", "\ \}"); }            if(!immediately && line.contains ("\" (function)) ) {immediately=true; Line= Line.replaceall ("\" \ \ (function), "\ \ (function")); }            if(Immediately && line.contains ("}) ()\"") ) {immediately=false; Line= Line.replaceall ("\\}\\" \ \ \ \ "", "\\}\\) \ \ \ \ \ \"); } Lines[i]=Line ; }        returnlines; }

Finally, you can look at the following end-up:

Talk about the pits that have been trampled during the use of Echarts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.