Because the project uses the data in the JSON format, search on the internet to Ali's Fastjson more useful, hereby record Fastjson usage, for later query.
Decode
First create a JSON parsing class:
Public class jsonparser { privateint devid;
Public int Getdevid () { return devid; } Public void setdevid (int devid) { this. devid = devid; }}
Create a corresponding object where parsing is needed:
String str = "{" Devid ": 12345}";
Jsonparser jsonstring = Json.parseobject (str, jsonparser. Class);
The str parameter is a string that needs parsing
Then simply call the getter () method of the object's response to get the value corresponding to the key:
int devid = Jsonstring.getdevid ();
System.out.println (devid);//12345
Encode
Also create a corresponding JSON class (for the sake of simplicity, take the example of the above parsing class)
Then create an object and call the appropriate setter () method:
New Jsonpaser (); Json.setdevid (12345);
After setting the value, call the JSON Library's toJSONString () method to generate the JSON-formatted data by passing in the object:
String str = json.tojsonstring (JSON); System.out.println (str); {"Devid": 12345}
PS: Through the test found that the function of Fastjson is more powerful, JSON format data is not fully compliant and can be resolved smoothly.
such as "{devid:12345}", "{" Devid ": 12345}", "{" Devid ":" 12345 "}" can parse correctly
How to use Fastjson to parse JSON-formatted data and generate JSON-formatted data