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