Java parsing of JSON

Source: Internet
Author: User

 First,JSON (JavaScript Object Notation) is a simple data format that is lighter than XML. JSON is constructed in two structures:
1, the collection of "name/value" pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative Array). Such as:
{
"Name": "Jackson",
"Age": 100
}


2, the value of the ordered list (an ordered list of values). In most languages, it is understood as an array (array) such as:
{
"Students":
[
{"Name": "Jackson", "Age": 100},
{"Name": "Michael", "Age": 51}
]
}
Second, Java parsing JSON step
A, server-side convert data to JSON string
First, the server-side project to import the JSON jar package and JSON depends on the jar package to the Builtpath path (these can be downloaded to json-lib official website: http://json-lib.sourceforge.net/)


The data is then converted to a JSON string, with the core function:
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 gets the JSON string
(because the JSON jar package is already integrated in the Android project, there is no need to import)
public class Httputil
{

public static string Getjsoncontent (String urlstr)
{
Try
{//Get HttpURLConnection Connection object
URL url = new URL (urlstr);
HttpURLConnection httpconn = (httpurlconnection) URL
. OpenConnection ();
Setting connection Properties
Httpconn.setconnecttimeout (3000);
Httpconn.setdoinput (TRUE);
Httpconn.setrequestmethod ("GET");
Get 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 equivalent to 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);
}
Converting a memory stream to a string
Jsonstr = new String (Out.tobytearray ());
}
catch (IOException E)
{
TODO auto-generated Catch block
E.printstacktrace ();
}
return jsonstr;
}
}
2. Get JavaBean
public static person Getperson (String jsonstr)
{
person person = new person ();
Try
{//Convert a JSON string to a JSON object
jsonobject jsonobj = n EW Jsonobject (JSONSTR);
//Get the specified JSON The value object of the Key object
jsonobject p Ersonobj = Jsonobj.getjsonobject ("person");
Gets all the properties of the 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
{To convert a JSON string to a JSON object
Jsonobj = new Jsonobject (JSONSTR);
Gets the value object for the specified JSON key object
Jsonarray personlist = Jsonobj.getjsonarray ("persons");
Traverse Jsonarray
for (int i = 0; i < personlist.length (); i++)
{
Get each JSON object
Jsonobject Jsonitem = Personlist.getjsonobject (i);
Gets 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;
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java parsing of JSON

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.