Net.sf.json.JSON convert JSON-only array to list

Source: Internet
Author: User
Tags array length object object

Net.sf.json processing a pure JSON array to convert to a list type

For example: {"UserIDs": [1000000,1000001]}, convert it to an array of integers

1  Packagecom.json.test;2 3 Importjava.util.List;4 5 ImportNet.sf.json.JSONArray;6 Importnet.sf.json.JSONException;7 ImportNet.sf.json.JSONObject;8 9  Public classJsontest {Ten  One      Public Static voidMain (string[] args) { AString json = "{\" userids\ ": [1000000,1000001]}"; -         Try { -Jsonobject json_test =Jsonobject.fromobject (JSON); theObject UserIDs = Json_test.get ("UserIDs"); -Jsonarray Jarray =Jsonarray.fromobject (userids); -List data =jarray.tolist (Jarray);//The method is obsolete -              for(Object object:data) { + System.out.println (object); -             } +             //Create a JSON parsing object (two rules: bracket with Jsonarray, note incoming data object) A             //Get Array length at             intLength =jarray.size (); -             //recall the way the array is evaluated? --->for loop through the array---> get the value -              for(inti = 0; i < length; i++) { -                 //This value is obtained by using the Get method of the type, based on the parsed data type, and the printout -String string =jarray.getstring (i); - System.out.println (string); in             } -}Catch(jsonexception e) { to         } +     } -  the}

Reference: in Java, string and JSON convert each other : http://blog.csdn.net/miaozhenzhong/article/details/52585726

Summary:

If the front-end incoming JSON object is accepted in development, it is recommended that the object be passed in, and the result will be sent out to the object, everything is Object!

Private list<integer> userids; it is more convenient to accept directly with an object.

Net.sf.json.JSON convert JSON-only array to list

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.