Java parsing JSON strings in multiple ways

Source: Internet
Author: User

The first kind: use Net.sf.json hard solution. Disadvantage: Requires field names and field names in JSON strings, field naming violates hump rules and is not conducive to expansion;

Jsonarray Jsonarray = Jsonarray.fromobject (str);
Jsonobject jobj = jsonarray.getjsonobject (0);
User user= (user) Jobj.tobean (jobj,user.class);

The second type: Parse the JSON string with Gson. Advantages: Simple writing, the field name as long as the use of @serializedname annotations, can be used to make the object field and JSON one by one corresponding; disadvantage: parsing a string such as a JSON array a little bit troublesome

Gson Gson = new Gson ();
User user= Gson.fromjson (msg, user.class);

The Third Kind: use Jackson to parse. Pros: As with Gson parsing, field names can use @jsonproperty annotations;

Objectmapper mapper = new Objectmapper ();

User user= mapper.readvalue (msg, user.class);

Java parsing JSON strings in multiple ways

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.