First look at a simple JSON-formatted data:
{data:{id:1,text: "This is Text", src: "Abc/abc/abc.png"},success:true}
When using JSON data to pass data to the front end, if there is a quotation mark in the data, if it cannot be escaped, then the foreground will get an incorrect JSON-formatted data, such as an object person:
classperson{PrivateString name; PrivateString desc; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString GetDesc () {returndesc; } Public voidsetdecs (String decs) { This. decs=decs; }}
If there is a person object, his desc is the He's Super man, his name is Jone
So if we need to convert this object to JSON data to the foreground, there are a number of ways we can do it:
1, the most flexible way, of course, is stitching up:
New StringBuffer (); Buffer.append ("{data:{' name ': '" person.getname () + "', ' desc ': '" person.getdesc () + "'}, Success:true} "); return buffer.tostring ();
In this case, the data received at the front desk is {data:{' name ': ' Jone ', ' desc ': ' He's Super man '},success:true} This is certainly not a problem;
However, if quotation marks appear in Desc, such as he is ' Super man ', if this is still the case, then the data is stitched together like this:
{data:{' name ': ' Jone ', ' desc ': ' He's ' Super Man '},success:true} in this case, it is not the standard JSON format data, the front desk can not be correctly parsed.
PS, some people say that the big deal without single quotes, double quotation marks as the JSON format of the stitching character, you can do it? What if the data is double-quoted: {data:{"name": "Jone", "desc": ' He is "Super man" "},success:true}
Even if the escape slash appears at the end of the data, what to do! {data:{' name ': ' jone\ ', ' desc ': ' He is \ ' Super man\ '},success:true}
In this case, the first method is not applicable, then you can look at the following:
2, using the serialization tool Jsonserializer, need jar package: Flexjson.jar
New Person (); Person.setname ("Jone"); Person.setdesc (new StringBuffer (). Append ("he is"). Append (' "'). Append (" The Super Man "). Append ('" '). ToString ()); New Jsonserializer (); System.out.println (serializer.serialize (person));
In this case, the data after serialization is this:
{"Class": "Test. Person "," desc ":" He is \ "Super man\", "name": "Jone"}
Such data, has been escaped, in the foreground can be correctly parsed.
But the flexibility of this approach is rather poor,
For example, if the data that the foreground needs, in addition to desc,name two fields, there is a need to person.address or other data to be processed, then the second way is not possible, then you can use the following way:
3, use Jsonobject object, this need jar package: Ezmorph.jar,json-lib.jar,xom.jar
The method is:
New Hashmap<string,object>(); Map.put ("name", Person.getname ()); Map.put ("DECs", Person.getdesc ()); Map.put ("age", a); Map.put ("home.address", "Street,road,china"); return
This results in the following: {"home.address": "Street,road,china", "decs": "He is \" Super man\ "," name ":" Jone "," Age ": 22}, also standard data.
Using this method, the JSON data can be escaped, and JSON data content processing is more flexible and diverse.
The above 3 methods, depending on the size of the project, generally speaking, more functions, database involved in foreign key, or data content diversification, it is best to take the following two ways.