JSON in Java

Source: Internet
Author: User

Import packages in 1.Java:

①commons-lang-2.5.jar

②commons-beanutils.jar

③commons-collections-3.2.1.jar

④commons-logging-1.1.1.jar

⑤ezmorph-1.0.6.jar

⑥json-lib-2.4-jdk15.jar

2. About Jsonarray and Jsonobject:

①jsonarray is a dynamic array, which individuals think is similar to the ArrayList category.

②jsonobject is a collection of key-value pairs that individuals think is similar to HashMap.

3. Since there are all these similar things in the collection, why use JSON? Personally think: I do not know, that is not ArrayList's ToString () method is also printed out its specific string content, why still use JSON ah?

4. OK, let's start the code:

import java.util.arraylist;import java.util.hashmap;import java.util.linkedhashmap;import  java.util.list;import java.util.listiterator;import java.util.map;import net.sf.json.jsonarray; import net.sf.json.jsonobject;public class json { public static void  Main (String[] args)  {  jsonobject jsobj1 = new jsonobject ();   jsobj1.put ("Gao",  "111");//Datang Name: Gaoshengi   jsobj1.put ("Xian",  "222");   Jsobj1.put ("Zhi",  "333")   system.out.println ("Jsonobject object constructed by the constructor 1:"  + jsobj1);   jsonobject jsobj2 = new jsonobject ();   jsobj2.put ("Gao",  "1111" );   jsobj2.put ("Xian",  "2222")   jsobj2.put ("Zhi",  "3333");   System.out.println ("Jsonobject object 2 constructed by the constructor:"  + jsobj2);   jsonobject jsobj3 =  new jsonobject ();   jsObj3.put ("Gao",  "11111")   jsobj3.put ("Xian",  "22222");   jsobj3.put ("Zhi",  "33333");   system.out.println ("Jsonobject object constructed by the constructor 3:"  + jsobj3);   jsonarray  jsarr1 = new jsonarray ();   jsarr1.add (jsObj1);   jsarr1.add (JSOBJ2);   jsarr1.add (JSOBJ3);   system.out.println ("Jsonarray object 1 constructed by the constructor:" &NBSP;+&NBSP;JSARR1);   string str = jsarr1.tostring ();   system.out.println (" Jsonarray Object 1 is converted to a String object after str: "+STR";   list<map<string, string>> listmap  = new ArrayList<Map<String, String>> (&NBSP;&NBSP;JSONARRAY&NBSP;JSARR2);  = jsonarray.fromobject (str); &NBSP;&NBSP;SYSTEM.OUT.PRINTLN (" JSON-formatted string STR converted to Jsonarray after Jsonarray object 2: "+JSARR2";  for  (int i = 0; i  < jsarr2.size ();  i++) &NBSP;{&NBSP;&NBSP;&NBSP;JSONOBJECT&NBSP;OBJ&NBSP;=&NBSp Jsonobject.fromobject (Jsarr2.get (i));    map<string, string> map = new  HashMap<String, String>    map.put ("Gao",  obj.getstring ("Gao"));    map.put ("Xian",  obj.getstring ("Xian")),    map.put ("Zhi",  obj.getstring (" Zhi "));    listmap.add (map);   }  system.out.println (" Jsonarray object 2JSARR2 converted to list<map<string, string>> object listmap: "+listmap";   System.out.println ("After parsing completed:");   listiterator iterator=listmap.listiterator (0);   while ( Iterator.hasnext ()) {   map<string, string> map = new hashmap< String, string> ();    map= (map<string, string>)  iterator.next ();    system.out.println (Map.get ("Gao")),    system.out.println (Map.get ("Xian"));    system.out.println (Map.get ("Zhi"));   } }} 

5. Summary: The individual writes this, is to realize the Java servlet Server transmits the data to the Android client, this just parses the JSON string in the Java file, as if the JSON inside the Android definition and in Java does not have the same, but the method is almost.

JSON in Java

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.