JSON parsing, json online Parsing

Source: Internet
Author: User
Tags tojson

JSON parsing, json online Parsing

1. API

Xxx getXxx (int index): obtains the corresponding element data in the json array according to the subscript.
Xxx optXxx (int index): obtains the corresponding element data in the json array according to the subscript.
Note:
The optXxx method returns an empty string or the default value you specified when the value of the corresponding key does not exist. However, the getString method returns a null pointer exception.

 

2. Special json data parsing

{& Quot; code & quot;: 0, & quot; list & quot;: {& quot; 0 & quot;: {& quot; aid & quot;: & quot; 6008965 & quot;, & quot; author & quot;: & quot ", "coins": 170, "copyright": "Copy", "create": ""}, "1": {"aid": "6008938 ", "author": "", "coins": 404, "copyright": "Copy", "create "}}}

 

public class FilmInfo {    private int code;    private List<FilmBean> list;    public static class FilmBean{        private String aid;        private String author;        private int coins;        private String copyright;        private String create;    }}

  

// Create the encapsulated Java object FilmInfo filmInfo = new FilmInfo (); // 2 parse jsontry {JSONObject jsonObject = new JSONObject (json); // parse int code = jsonObject at the first layer. optInt ("code"); JSONObject list = jsonObject. optJSONObject ("list"); // The first layer encapsulates filmInfo. setCode (code); List <FilmInfo. filmBean> lists = new ArrayList <> (); filmInfo. setList (lists );
// Second layer resolution for (int I = 0; I <list. length (); I ++) {JSONObject jsonObject1 = list. optJSONObject (I + ""); if (jsonObject1! = Null) {String aid = jsonObject1.optString ("aid"); String author = jsonObject1.optString ("author"); int coins = jsonObject1.optInt ("coins "); string copyright = jsonObject1.optString ("copyright"); String create = jsonObject1.optString ("create"); // Layer 2 Data encapsulation FilmInfo. filmBean filmBean = new FilmInfo. filmBean (); filmBean. setAid (aid); filmBean. setAuthor (author); filmBean. setCoins (coins); filmBean. setCopyright (copyright); filmBean. setCreate (create); lists. add (filmBean) ;}} catch (JSONException e) {e. printStackTrace ();}

  

3. GsonFramework Technology

(1) convert the string {} In json format to a Java object
API:
FromJson (String json, Class <T> classOfT );

Step 1) import the jar package of Gson to the project. 2) create a Gson object: Gson gson = new Gson (). 3) Call the fromJson () method through the created Gson object, returns the Java object corresponding to the JSON data: ShopInfo shopInfo = gson. fromJson (json, ShopInfo. class );

 

Gson gson = new Gson();ShopInfo shopInfo = gson.fromJson(json, ShopInfo.class);

(2) convert a json string [] to a List of Java objects
API:
FromJson (String json, Type typeOfT );

Gson gson = new Gson();List<ShopInfo> shops = gson.fromJson(json, new TypeToken<List<ShopInfo>>() {}.getType());

(3) convert a Java object to a json string {}
API:
String toJson (Object src );

// 1 obtain or create the Java object ShopInfo shopInfo = new ShopInfo (1, "abalone", 250.0, "baoyu "); // 2 generate the JSON data Gson gson = new Gson (); String json = gson. toJson (shopInfo );

(4) convert the Java object List to a json string []

API:
String toJson (Object src );

// 1 obtain or create a Java object List <ShopInfo> shops = new ArrayList <> (); ShopInfo baoyu = new ShopInfo (1, "abalone", 250.0, "baoyu "); shopInfo longxia = new ShopInfo (2, "Lobster", 251.0, "longxia"); shops. add (baoyu); shops. add (longxia); // 2 generate the JSON data Gson gson = new Gson (); String json = gson. toJson (shops );

 

4. FastJson framework technology
(1) convert the string {} In json format to a Java object
API:
ParseObject (String json, Class <T> classOfT );

ShopInfo shopInfo = JSON.parseObject(json, ShopInfo.class);

(2) convert a json string [] to a List of Java objects
API:
List <T> parseArray (String json, Class <T> classOfT );

List<ShopInfo> shopInfos = JSON.parseArray(json, ShopInfo.class);

(3) convert a Java object to a json string {}
API:
String toJSONString (Object object );
Steps:
1) import the fastjson jar package
2) JSON calls the toJSONString () method to obtain the converted json data
For example:

ShopInfo shopInfo = new ShopInfo (1, "abalone", 250.0, "baoyu"); String json = JSON. toJSONString (shopInfo );

(4) convert the Java object List to a json string []
API:
String toJSONString (Object object );

List <ShopInfo> shops = new ArrayList <> (); ShopInfo baoyu = new ShopInfo (1, "abalone", 250.0, "baoyu"); ShopInfo longxia = new ShopInfo (2, "Lobster", 251.0, "longxia"); shops. add (baoyu); shops. add (longxia); String json = JSON. toJSONString (shops );

  

 

Related Article

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.