JSON parsing, json online Parsing

Source: Internet
Author: User

JSON parsing, json online Parsing

I,JSON (JavaScript Object Notation) is a simple data format, which is lighter than xml.
Json is constructed in two structures:
1. A collection of name/value pairs ). In different languages, it is understood as an object, record, struct, dictionary, and hash table ), keyed list or associative array ). For example:
{
"Name": "LiJinlun ",
"Age": 25
}


2. An ordered list of values ). In most languages, it is understood as an array, such:
{
"Students ":
[
{"Name": "LiJinlun", "age": 25 },
{"Name": "liudehua", "age": 51}
]
}

Ii. JSON parsing steps in java

A. The server converts the data to A json string.
First, the server project needs to import the json jar package and the jar package on which the json depends to the builtPath.

(These can be downloaded to the JSON-lib Official Website: http://json-lib.sourceforge.net /)

Then convert the data into a json string. The core function is:
Public static String createJsonString (String key, Object value)
{
JSONObject jsonObject = new JSONObject ();
JsonObject. put (key, value );
Return jsonObject. toString ();
}

B. The client converts the json string to the corresponding javaBean
1. The client obtains the json string.(Because the android project has integrated the json jar package, you do not need to import it here)
Public class HttpUtil
{

Public static String getJsonContent (String urlStr)
{
Try
{// Obtain the HttpURLConnection connection object
URL url = new URL (urlStr );
HttpURLConnection httpConn = (HttpURLConnection) url
. OpenConnection ();
// Set Connection Properties
HttpConn. setConnectTimeout (3000 );
HttpConn. setDoInput (true );
HttpConn. setRequestMethod ("GET ");
// Obtain the corresponding code
Int respCode = httpConn. getResponseCode ();
If (respCode = 200)
{
Return ConvertStream2Json (httpConn. getInputStream ());
}
}
Catch (MalformedURLException e)
{
// TODO Auto-generated catch block
E. printStackTrace ();
}
Catch (IOException e)
{
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return "";
}


Private static String ConvertStream2Json (InputStream inputStream)
{
String jsonStr = "";
// ByteArrayOutputStream is equivalent to the Memory output stream
ByteArrayOutputStream out = new ByteArrayOutputStream ();
Byte [] buffer = new byte [1024];
Int len = 0;
// Transfer the input stream to the Memory output stream
Try
{
While (len = inputStream. read (buffer, 0, buffer. length ))! =-1)
{
Out. write (buffer, 0, len );
}
// Swap the memory flow into a string
JsonStr = new String (out. toByteArray ());
}
Catch (IOException e)
{
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return jsonStr;
}
}

2. Obtain javaBean
Public static Person getPerson (String jsonStr)
{
Person person = new Person ();
Try
{// Convert a json string to a json object
JSONObject jsonObj = new JSONObject (jsonStr );
// Obtain the value object of the specified json key object
JSONObject personObj = jsonObj. getJSONObject ("person ");
// Obtain all attributes of an object
Person. setId (personObj. getInt ("id "));
Person. setName (personObj. getString ("name "));
Person. setAddress (personObj. getString ("address "));
}
Catch (JSONException e)
{
// TODO Auto-generated catch block
E. printStackTrace ();
}

Return person;
}

Public static List <Person> getPersons (String jsonStr)
{
List <Person> list = new ArrayList <Person> ();

JSONObject jsonObj;
Try
{// Convert a json string to a json object
JsonObj = new JSONObject (jsonStr );
// Obtain the value object of the specified json key object
JSONArray personList = jsonObj. getJSONArray ("persons ");
// Traverse jsonArray
For (int I = 0; I <personList. length (); I ++)
{
// Obtain every json object
JSONObject jsonItem = personList. getJSONObject (I );
// Obtain the value of each json object
Person person = new Person ();
Person. setId (jsonItem. getInt ("id "));
Person. setName (jsonItem. getString ("name "));
Person. setAddress (jsonItem. getString ("address "));
List. add (person );
}
}
Catch (JSONException e)
{
// TODO Auto-generated catch block
E. printStackTrace ();
}

Return list;
}

 

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.