Public classa11111111 {//The parameter obj can be a JSON object, a string, a list Public Static voidFun (Object obj) {jsonarray data=jsonarray.fromobject (obj); for(intI=0; I<data.size (); i++) {Jsonobject jobj= (jsonobject) data.Get(i); System. out. println (Jobj); /** *{"aid": 3201, "name": "Brand", "CID": "1351"} {"Aid": 7716, "name": "Type", "CID": "1351"} {"Aid": 8668, "name": "Price", "CID": "1351"}*/String name= (String) jobj.Get("name"); System. out. println (name);//Brand Type Price } } Public Static voidMain (string[] args) {
Making a string
//String list = Request.getparameter ("json");String STR9 ="[{\ "aid\": 3201,\ "name\": \ "brand \", \ "cid\": \ "1351\"}, {\ "aid\": 7716,\ "name\": \ "type \", \ "cid\": \ "1351\"}, {\ "aid\" : 8668,\ "name\": \ "price \", \ "cid\": \ "1351\"}]" ; Fun (STR9); //Make a listList<object> list90 =NewArraylist<object>(); Jsonarray Data=Jsonarray.fromobject (STR9); for(intI=0; I<data.size (); i++) {Jsonobject jobj= (jsonobject) data.Get(i); List90.add (Jobj); //turn a JSON array into a list /** *{"aid": 3201, "name": "Brand", "CID": "1351"} {"Aid": 7716, "name": "Type", "CID": "1351"} {"Aid": 8668, "name": "Price", "CID": "1351"}*/} fun (LIST90); //The 2 functions that are called are the same as the resultsString str_0=list90.tostring (); Fun (STR_0); //The results are all the same. } }
Conversion of list and JSON arrays (source code)