Commonly used JSON data parsing format

Source: Internet
Author: User

public static person Getperson (String key,string jsonstring) {Person person = new person ();            try {jsonobject jsonobject = new Jsonobject (jsonstring);            Jsonobject personobject = jsonobject.getjsonobject ("person");            Person.setid (Personobject.getint ("id"));            Person.setname (personobject.getstring ("name"));        Person.setaddress (personobject.getstring ("Address"));    } catch (Exception e) {//Todo:handle Exception} return person; } public static list<person> getpersons (String key,string jsonstring) {list<person> List = new Array        List<person> ();            try {jsonobject jsonobject = new Jsonobject (jsonstring);            Returns the JSON array Jsonarray Jsonarray = Jsonobject.getjsonarray (key);                for (int i = 0; i < jsonarray.length (); i++) {Jsonobject jsonObject2 = Jsonarray.getjsonobject (i); Person person = new PersOn ();                Person.setid (Jsonobject2.getint ("id"));                Person.setname (jsonobject2.getstring ("name"));                Person.setaddress (jsonobject2.getstring ("Address"));            List.add (person);    }} catch (Exception e) {//Todo:handle Exception} return list; } public static list<string> getList (String key,string jsonstring) {list<string> List = new Arraylis        T<string> ();            try {jsonobject jsonobject = new Jsonobject (jsonstring);            Jsonarray Jsonarray = Jsonobject.getjsonarray (key);                for (int i = 0; i < jsonarray.length (); i++) {String msg = jsonarray.getstring (i);            List.add (msg);    }} catch (Exception e) {//Todo:handle Exception} return list; } public static list<map<string, object>> getlistmap (String key,string jsonstring) {list<map<s Tring, object>> list = new arraylist<map<string,object>> ();            try {jsonobject jsonobject = new Jsonobject (jsonstring);            Jsonarray Jsonarray = Jsonobject.getjsonarray (key);                for (int i = 0; i < jsonarray.length (); i++) {Jsonobject jsonObject2 = Jsonarray.getjsonobject (i);                map<string, object> map = new hashmap<string, object> ();                iterator<string> Iterator = Jsonobject2.keys ();                    while (Iterator.hasnext ()) {String Json_key = Iterator.next ();                    Object Json_value = Jsonobject2.get (Json_key);                    if (Json_value = = null) {Json_value = "";                } map.put (Json_key, Json_value);            } list.add (map);    }} catch (Exception e) {//Todo:handle Exception} return list; }
  
  
用Struts框架,struts.xml里配置继承json-defual,一般Struts包都有,在action下个方法,一样的返回结果 struts.xml中
<result name="返回字符串" type="json">  <param name="includeProperties">传出去的值,传出去的值,...</param>   </result>

Commonly used JSON data parsing format

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.