How Java parses multiple layers of JSON data

Source: Internet
Author: User

1. Use standard JSON objects, such as org.json.JSONObject json = new Org.json.JSONObject (yourjsonstring), and then parse one by one by Get (keystring)
2. Construct a class MyObject, according to your question, which adds String name,list<myobject> child, two attributes, add Getter Setter method, Parsing is done through Com.google.gson.Gson. The steps are as follows:
String jsonstr = "{' name ': ' 111 ', ' child ': [{' Name ': ' 222 ', ' child ': ' {' name ': ' 333 '}]},{' name ': ' 2221 '}]}";
Gson Gson = new Gson ();
MyObject obj = Gson.fromjson (jsonstr,myobject.class);

1 Importjava.util.List;2 3 ImportCom.google.gson.Gson;4 5  Public classMyObject {6 String name;7List<myobject>Child ;8      PublicString GetName () {9         returnname;Ten     } One      Public voidsetName (String name) { A          This. Name =name; -     } -      PublicList<myobject>Getchild () { the         returnChild ; -     } -      Public voidSetChild (list<myobject>Child ) { -          This. Child =Child ; +     } -      +      Public Static voidMain (string[] args) { AString jsonstr = "{' name ': ' 111 ', ' child ': [{' Name ': ' 222 ', ' child ': ' {' name ': ' 333 '}]},{' name ': ' 2221 '}]}"; atGson Gson =NewGson (); -MyObject obj = Gson.fromjson (jsonstr,myobject.class); -System.out.println (Obj.getchild (). Get (0). GetName ());//--222 -System.out.println (Obj.getchild (). Get (1). GetName ());//--222 -System.out.println (Obj.getchild (). Get (0). Getchild (). Get (0). GetName ());//--333 -     } in}

Quote from http://www.iteye.com/problems/88002 flashsnow, thank you very much.

How Java parses multiple layers of JSON 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.