Java Operation Json

Source: Internet
Author: User

1. Building JSON with Gson

Initialization

New Jsonobject ();

Add Property

Jsonobject.addproperty ("Name", Boolean/character/number/string);

To add a sub-level Json

Jsonobject.add ("json_1", jsonobject);

Get JSON element properties

Jsonobject.get ("name"). Getasstring ();

Using Long.parselong Integer.parseint

Get JSON sub-JSON

Jsonobject.getasjsonobject ("json_1");

Array

New Jsonarray ();

Array operations Add get

Jsonarray.add (element); Jsonarray.get (i); // add and get correlation functions

Iterating through an array

 for (int i = 0; i < jsonarray.size (); i++) {     = (jsonelement) jsonarray.get (i);      // take int      jsonelement.getasint ();      // and so on other type get}
2. Using Fastjson to manipulate JSON

Initialization

New Jsonobject ();

Add to

Jsonobject.put ("name", Object);

Get Child Jsonobject

Jsonobj.getjsonobject ("name");

Get Child Jsonarray

Jsonobj.getjsonarray ("name");

JSON array Additions get

JsonArray.addjsonArray.get

JSON array traversal operations

 for (int i = 0; i < jsonarray.size (); i++) {    = (jsonobject) jsonarray.get (i);     // take int    Jsonobject.getintvalue ("name");     // Fetch string    Jsonobject.getstring ("name");  //  etc other type get}

Java Operation Json

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.