Parse nested JSON strings in Java.

Source: Internet
Author: User

The JSON string here is an example of the JSON Format String above the public time line.

Http://api.fanfou.com/statuses/public_timeline.json

Currently, HTTP basic verification is still used. If you are not clear about how to log on and obtain data through the APIS, refer to my

Http://blog.csdn.net/w59879213/archive/2011/01/14/6140523.aspx

This article describes how to perform HTTP basic verification and obtain the returned data using APIs that are not supported.

 

The premise of the project here is that the data has been obtained and the data string is JSON

 

The android development environment includes the org. JSON package,

However, if you create a Java project, org. JSON package. you can search for JSON. JAR file, right-click your JRE system library-> build path and add it to your project.

 

Add

Import org. JSON. jsonarray; <br/> Import org. JSON. jsonexception; <br/> Import org. JSON. jsonobject;

 

The parsing steps are actually very simple.

First, get the array of all data through the jsonarray object,

Jsonarray array = new jsonarray (JSON );

The example of "meal no" is the information of all speeches, but the information of each statement of "meal no" is nested with a JSON string of user information.

Therefore, when obtaining the information of each statement, you need to obtain the JSON string marked with user again.

 

Jsonobject object = array. getjsonobject (I); <br/> // obtain the nested JSON string <br/> string users = object. getstring ("user"); <br/> // explain the nested JSON string to obtain the usable object. <br/> jsonobject user = new jsonobject (users ); 

 

 

After parsing, you can use the getstring ("key") method of the object to obtain the corresponding value ~

 

The Code is as follows:

// Obtain all JSON strings. <br/> jsonarray array = new jsonarray (JSON); <br/> for (INT I = 0; I <array. length (); I ++) <br/>{< br/>/* obtain the list of the first record from the list, including the information ID, sender... <br/> The list contains another JSON string */<br/> jsonobject object = array. getjsonobject (I); <br/> // obtain the nested JSON string <br/> string users = object. getstring ("user"); <br/> // explain the nested JSON string to obtain the usable object. <br/> jsonobject user = new jsonobject (users); <br/> system. out. println (object. getstring ("ID") + ":" + User. getstring ("Birthday"); <br/>} 

 

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.