How to use Fastjson to parse JSON-formatted data and generate JSON-formatted data

Source: Internet
Author: User

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

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.