A JSON string from the server, how can it be called as a JavaScript object (JSON object) on the Web side?
1, if using jquery, it is very convenient, you can in the AJAX series of functions, the parameter datatype JSON can be, the returned data is a JSON object.
PS: If you want to process the form as a JSON string, you can use. Serialize () and. Serializearray () processing, if you want to call as a URL, you can use Jquery.param () processing.
2, $.parsejson (JSONSTR)
3. Json.parse (JSONSTR) from the browser
4. Using the string transcoding function
Eval (' (' + jsonstr + ') '); (not recommended, code executed)
5, using the official JSON json.js
Parse () method
Second, JSON object to string
1, json.stringify (jsonobj);
2, Obj.tojsonstring ()
Transferring to Java objects
1.//Convert the list of Java objects to a JSON object array and turn to a string
Jsonarray array = jsonarray.fromobject(userlist);
String jsonstr = array.tostring ();
2.//Converts a Java object into a JSON object and converts it into a string
Jsonobject object = jsonobject.fromobject(invite);
Log4jInit.ysulogger.debug (Object.ToString ());
3.//converting a JSON string to an array of Java objects
String personstr = Getrequest (). GetParameter ("persons");
Jsonarray json = Jsonarray.fromobject (PERSONSTR);
list<invoidperson> persons = (list<invoidperson>)jsonarray.tocollection(JSON, Nvoidperson.class);
4.//to convert a JSON string to a Java object
str = "{\" lendperson\ ": \" John Doe \ ", \" lendcompany\ ": \" limited \ ", \" checkperson\ ": \" John Doe \ ",
\ "lenddate\": \ "2010-07-19t00:00:00\", \ "lendcounts\": 4,\ "
Passports\ ": [{\" passportid\ ": \" d\ ", \" name\ ": \" Li Shuichang \ ", \" passporttype\ ": \" K\ "},
{\ "passportid\": \ "k9051\", \ "name\": \ "Li ping \", \ "passporttype\": \ "K\"},
{\ "passportid\": \ "k90517\", \ "name\": \ "Yuan Han mei \", \ "passporttype\": \ "K\"},
{\ "passportid\": \ "k905199\", \ "name\": \ "He Ming \", \ "passporttype\": \ "K\"}]} ";
Jsonobject jsonobject = Jsonobject.fromobject (str);
Passportlendsentity passportlends = null;
try {
Get a JSON array
Jsonarray array = Jsonobject.getjsonarray ("passports");
Convert JSON arrays to list<passportforlendsentity> generics
list<passportforlendsentity> list = new arraylist<passportforlendsentity> ();
for (int i = 0; i < array.size (); i++) {
Jsonobject object = (jsonobject) array.get (i);
Passportforlendsentity Passport = (passportforlendsentity)Jsonobject.tobean(Object,
Passportforlendsentity.class);
if (passport! = null) {
List.add (Passport);
}
}
Convert passportlendsentity entity class
Passportlends = (passportlendsentity)Jsonobject.tobean(Jsonobject, Passportlendsentity.class);
JSON to JS object, JS object to JSON