Review parsing nested JSON

Source: Internet
Author: User

Some of the classic examples found on the web are reviewed using

One, parsing nested JSON strings, nesting another JSON string in a JSON string

{"msg": {"rescode": "0000","attrname": "Sensordata""result":{ "food1" : 0, "water": 0, "FOOD2": 106, "humidity": "152.42", "temprature": "103.65"}} 


What I want to get is all the data in result nested in MSG.

First, create the Bean class with MSG and result, and use result as one of the variables of MSG

 Public classSensorbeanImplementsSerializable {Privateresult result;Private intRescode;PrivateString attrname; PublicResult GetResult () {returnresult;} Public voidSetresult (Result result) { This. result =result;} Public intGetrescode () {returnRescode;} Public voidSetrescode (intRescode) { This. Rescode =Rescode;} PublicString Getattrname () {returnAttrname;} Public voidsetattrname (String attrname) { This. Attrname =Attrname;} @Override PublicString toString () {return"msg{" + "result=" + result.tostring () + ", rescode=" + Rescode + ", attrname= '" + attrname + ' \ ' + '} ';}}

Two classes inherit serialization

 Public classResultImplementsserializable{/*** food1:0* water:0* food2:106* humidity:152.42* temprature:103.65*/Private intfood1;Private intwater;Private intfood2;PrivateString Humidity;PrivateString temprature; Public intgetFood1 () {returnfood1;} Public voidSETFOOD1 (intfood1) { This. food1 =food1;} Public intGetwater () {returnWater;} Public voidSetwater (intwater) { This. Water =Water;} PublicString gettemprature () {returntemprature;} Public voidsettemprature (String temprature) { This. temprature =temprature;} @Override PublicString toString () {return"result{" + "food1=" + Food1 + ", water= '" + Water + ", temprature=" + temprature+ ' \ ' + '} ';}}

And then perform Gson parsing where the data is obtained.

Gson Gson = new Gson ();
Sensorbean Sensorbean = Gson.fromjson (data.getstring ("msg"), Sensorbean.class);
Result result = Sensorbean.getresult ();
L.D (TAG, "Result.bean =" + result.tostring ());

Two. Add JSON nesting of arrays

{            "Error": "0",            "Data": [                {                    "Name": "1",                    "Value": [                        {                            "Name": "2",                            "Value": "3"                        },                        {                            "Name": "4",                            "Value": "5"                        }                    ]                },                {                    "Name": "6",                    "Value": [                        {                            "Name": "7",                            "Value": "8"                        },                        {                            "Name": "9",                            "Value": "10"                        }                    ]                }            ]        }
Importjava.util.ArrayList; Public classJsonbean {Privateerror Error; //data is a number of groups     PrivateArrayList data;  PublicError GetError () {returnerror;}  Public voidSetError (Error error) { This. Error =error;}  PublicArrayList GetData () {returndata;}  Public voidsetData (ArrayList data) { This. data =data;}  Public Static classBean2 { PublicString name;  PublicArrayList value;  PublicString GetName () {returnname; }          Public voidsetName (String name) { This. Name =name; }          PublicArrayList GetValue () {returnvalue; }          Public voidSetValue (ArrayList value) { This. Value =value; } @Override PublicString toString () {return"Bean2 [name=" + name + ", value=" + value + "]"; }     }      Public Static classBEAN3 { PublicString name; PrivateString value;  PublicString GetName () {returnname; }          Public voidsetName (String name) { This. Name =name; }          PublicString GetValue () {returnvalue; }          Public voidSetValue (String value) { This. Value =value; } @Override PublicString toString () {return"Bean3 [name=" + name + ", value=" + value + "]"; }     }}  }

Review parsing nested 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.