Java Json.org.jar

Source: Internet
Author: User

Servlet we can think of him as a data medium, and he finally executes the method body to get the processed data back to the requesting client, or in XML format, or in JSON format, I'm using JSON format data, So here's what I'm going to say Org.json.jar this library and the way I encapsulate the return data.

This library has two core classes of->jsonobject and Jsonarray

First, Jsonobject

Jsonobject This class is equivalent to the nsdictionary in iOS, after which the data is presented to the client callers in the form of a key-value pair, but here we return the JSON character, so we need to first jsonobject. ToString () , then iOS can convert the JSON string to a dictionary nsdictioanry and then value it as a key-value pair (for example: NSString *name=[nsdictionary objectforkey ("name")] , since Android is developed in Java, Android developers can use this class directly to reverse the retrieved JSON string back into the Jsonobject class (String Name=jsonobject.get ("name"))

Below I paste the database resultset conversion to Jsonobject method, here is a key point is jsonobject accept is actually an object, he is not an array collection, so here how the database returned

ResultSet has multiple data he can only convert the first data to it, you can actually think of Jsonarray as a generic collection in Java, he holds the class is Jsonobject class.
 Public StaticJsonobject Resultsettojsonobject (ResultSet rs)throwssqlexception,jsonexception {//JSON ObjectJsonobject jsonobj =NewJsonobject ();//get number of columnsResultSetMetaData MetaData =Rs.getmetadata ();intColumnCount =Metadata.getcolumncount ();//traverse each piece of data in the ResultSetif(Rs.next ()) {//iterate through each column for(inti = 1; I <= ColumnCount; i++) {String columnName=Metadata.getcolumnlabel (i); String value=rs.getstring (columnName); Jsonobj.put (columnName, value);} } returnjsonobj;}

Second, Jsonarray

Jsonarray This class is equivalent to the Nsarray in iOS, the conversion is the collection of data presented to the client callers, of course, here we return the JSON character, so we need to first Jsonarray. ToString (), iOS can then convert this JSON string into a dictionary nsarray, and then iterate through each member of the collection (for example: for (int i=0;i<nsarray.count;i++)
{
Nsdictionary *dic=[nsarray OBJECTATINDEX:I];

NSString *name=[dic Objectforkey ("name")];

}
, since Android is developed in Java, Android developers can use this class directly, reverse the retrieved JSON string back into Jsonarray and iterate through the values:

for (int i = 0; I <JsonArray.length; i++) {//traverse Jsonarray

Jsonobject OJ = Jsonarray.getjsonobject (i);

String name=oj.get ("name");

}

In fact, Jsonarray inside the member is Jsonobject, here can also create a class to accept the value, add to list<> inside to do a generic collection.

Below I paste database resultset convert to Jsonobject method

     Public StaticJsonarray Resultsettojsonarry (ResultSet rs)throwssqlexception,jsonexception {//JSON ArrayJsonarray array =NewJsonarray (); //get number of columnsResultSetMetaData MetaData =Rs.getmetadata (); intColumnCount =Metadata.getcolumncount (); //traverse each piece of data in the ResultSet             while(Rs.next ()) {Jsonobject jsonobj=NewJsonobject (); //iterate through each column                 for(inti = 1; I <= ColumnCount; i++) {String columnName=Metadata.getcolumnlabel (i); String value=rs.getstring (columnName);                  Jsonobj.put (columnName, value);               } array.put (Jsonobj); }                          returnArray; } 

Third, the combination of a common JSON

I am now a combination of this, the converted JSON string is converted from a jsonobejct, jsonobject inside has put two team values, one type is Jsonobject, the other is the list data

Jsonarray, of course, this is just my combination, this combination is best based on the actual needs of the project,

protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html"); Response.setcharacterencoding ("GB2312"); PrintWriter out=Response.getwriter (); String[] Mysqlparameter=Newstring[]{}; ResultSet Returndata=mysqlhepler.executequery ("SELECT * FROM InfoSheet", Mysqlparameter);          Jsonarray Array; Try {             //The outermost jsonobject of the JSONJsonobject masterjsonobject=NewJsonobject (); //JSON internal list data--here is the data returned from the databasearray=Resulttojsontool.resultsettojsonarry (returndata); //Here's another jsonobject that holds the sub-data .Jsonobject songjsonobject=NewJsonobject (); Songjsonobject.put ("Token", "12345678"); Songjsonobject.put ("UserName", "Xiaoming"); Songjsonobject.put ("Usertype", "2"); //list data and sub jsonobjectput to MasterjsonobjectMasterjsonobject.put ("DataList", array); Masterjsonobject.put ("Shareobject", Songjsonobject);        Out.println (Masterjsonobject.tostring ()); } Catch(SQLException |jsonexception E1)        {E1.printstacktrace (); }           }

Java Json.org.jar

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.