Gson for Android

Source: Internet
Author: User

{

"ID": 912345678901,

"Age": 33,

"Text": "How do I stream JSON in Java? ",

"Geo": NULL,
}

If the red part indicates that it may be null, the following name. Equals ("Geo") & reader. Peek () are required during the judgment ()! = Jsontoken. null

If you do not want to parse the JSON field in the orange part, you need to add an else statement at the time of judgment as follows: else {reader. skipvalue ()}

 

Then there are some examples, all of which are JSON combinations and updated from time to time.

1.The actual JSON data is followed by the corresponding code content.

{

"ID": 912345678901, "text": "How do I stream JSON in Java? "," Geo ": NULL," user ": {" name ":" json_newb "," followers_count ": 41 }} reader. beginobject ();
While (reader. hasnext ()){
String name = reader. nextname ();
If (name. Equals ("ID ")){
System. Out. println ("ID:" + reader. nextlong ());
} Else if (name. Equals ("text ")){
System. Out. println ("text:" + reader. nextstring ());
} Else if (name. Equals ("Geo") & reader. Peek ()! = Jsontoken. null ){
System. Out. println ("text:" + reader. nextstring ());
} Else if (name. Equals ("user ")){
Usernameobject ();
} Else {
Reader. skipvalue ();
}
}
Reader. endobject ();

Private void usernameobject () throws ioexception {
Reader. beginobject ();
While (reader. hasnext ()){
String nxtname = reader. nextname ();
If (nxtname. Equals ("name ")){
System. Out. println ("nxtname:" + reader. nextstring ());
} Else if (nxtname. Equals ("followers_count ")){
System. Out. println ("nxtname:" + reader. nextint ());
}

}

2.

[{"ID": 912345678901, "text": "How do I stream JSON in Java? "," Geo ": NULL," user ": {" name ":" json_newb "," followers_count ": 41 },{" ID ": 777777777888," text ": "dfngsdnglnsldfnsl", "Geo": NULL, "user": {"name": "dsfgpd", "followers_count": 24 }}

]

Reader. beginarray ();
While (reader. hasnext ()){
Reader. beginobject ();
While (reader. hasnext ()){
String name = reader. nextname ();
If (name. Equals ("ID ")){
System. Out. println ("ID:" + reader. nextlong ());
} Else if (name. Equals ("text ")){
System. Out. println ("text:" + reader. nextstring ());
} Else if (name. Equals ("Geo") & reader. Peek ()! = Jsontoken. null ){
System. Out. println ("text:" + reader. nextstring ());
} Else if (name. Equals ("user ")){
Usernameobject ();
} Else {
Reader. skipvalue ();
}
}
Reader. endobject ();
}

Reader. endarray ();

3.

{"Statuses": [{"ID": 912345678901, "text": "How do I stream JSON in Java? "," Geo ": NULL," user ": {" name ":" json_newb "," followers_count ": 41 },{" ID ": 777777777888," text ": "dfngsdnglnsldfnsl", "Geo": NULL, "user": {"name": "dsfgpd", "followers_count": 24}]

}

Reader. beginobject ();
While (reader. hasnext ())
{
String begin = reader. nextname ();
If (begin. Equals ("statuses "))
{
Array ();
}
}
Reader. endobject ();

Private void array () throws ioexception {
Reader. beginarray ();
While (reader. hasnext ()){
Reader. beginobject ();
While (reader. hasnext ()){
String name = reader. nextname ();
If (name. Equals ("ID ")){
System. Out. println ("ID:" + reader. nextlong ());
} Else if (name. Equals ("text ")){
System. Out. println ("text:" + reader. nextstring ());
} Else if (name. Equals ("Geo") & reader. Peek ()! = Jsontoken. null ){
System. Out. println ("text:" + reader. nextstring ());
} Else if (name. Equals ("user ")){
Usernameobject ();
} Else {
Reader. skipvalue ();
}
}
Reader. endobject ();
}
Reader. endarray ();
}

Private void usernameobject () throws ioexception {
Reader. beginobject ();
While (reader. hasnext ()){
String nxtname = reader. nextname ();
If (nxtname. Equals ("name ")){
System. Out. println ("nxtname:" + reader. nextstring ());
} Else if (nxtname. Equals ("followers_count ")){
System. Out. println ("nxtname:" + reader. nextint ());
}
}
Reader. endobject ();

}

 

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.