Java operation Json, java operation json

Source: Internet
Author: User

Java operation Json, java operation json
1. Use Gson to build Json

Initialization

JsonObject jsonObject = new JsonObject();

Add attribute

JsonObject. addProperty ("name", Boolean/character/number/string );

Add sub-level Json

jsonObject.add("json_1", jsonObject);

Get json element attributes

jsonObject.get("name").getAsString();

Use Long. parseLong Integer. parseInt

Obtain json Sub-Level Json

jsonObject.getAsJsonObject("json_1");

Array

JsonArray jsonArray = new JsonArray();

 

Array Operation add get

JsonArray. add (element); jsonArray. get (I); // add and get Functions

 

Traverse Arrays

For (int I = 0; I <jsonArray. size (); I ++) {JsonElement jsonElement = (JsonElement) jsonArray. get (I); // get int jsonElement. getAsInt (); // and other types of get}
2. Use fastjson to operate Json

Initialization

JSONObject jsonObject = new JSONObject();

 

Add

jsonObject.put("name",Object);

 

Get the child-level JSONObject

jsonObj.getJSONObject(“name”);

 

Get Child-level JSONArray

jsonObj.getJSONArray("name");

 

Add and retrieve json Array

jsonArray.addjsonArray.get

 

Json array Traversal

For (int I = 0; I <jsonArray. size (); I ++) {JSONObject jsonObject = (JSONObject) jsonArray. get (I); // get int jsonObject. getIntValue ("name"); // string jsonObject. getString ("name"); // and other types of get}

 

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.