JSON string conversion methods of JSONObject and JSONArray, jsonjsonobject
1. Download jsonGo to http://www.json.org/to find the java-jsondownload file and put it in the project source code. Then, you can use its class object.
II. Specific conversion process
// JSONObjectString jsonMessage = "{\" \ ": \" 88 \ ", \" Mathematics \ ": \" 78 \ ", \" computer \": \ "99 \"} "; String value1 = null; try {// convert the String to the jsonObject object JSONObject myJsonObject = new JSONObject (jsonMessage ); // obtain the value value1 = myJsonObject. getString ("Mathematics");} catch (JSONException e) {} System. out. println ("value1 =" + value1); // JSONArrayjsonMessage = "[{'num': 'score ', 'foreign language': 88, 'historical ': 65, 'geolocation ': 99, 'object': {'aaa': '000000', 'bbb': '000000', 'cccccc': '000000 '}}, "+" {'num': 'interest ', 'foreign language': 28, 'hire': 45, 'geolocation ': 19, 'object': {'aaa ': '11a11', 'bbb ': '000000', 'cccc': '000000' }}, "+" {'num': 'hobby', 'foreign language ': 48, 'hire': 62, 'geolocation ': 39, 'object': {'aaa': '11c11', 'bbb': '123', 'cccccc ': '20140901'}] "; JSONArray myJsonArray; try {myJsonArray = new JSONArray (jsonMessage); for (int I = 0; I <myJsonArray. length (); I ++) {// get every JsonObject JSONObject myjObject = myJsonArray. getJSONObject (I); // obtain the value String numString = myjObject in each object. getString ("num"); int englishScore = myjObject. getInt ("Foreign Language"); int historyScore = myjObject. getInt ("History"); int geographyScore = myjObject. getInt ("geographic"); // get the object JSONObject myjObject2 = myjObject in the array. getJSONObject ("object"); String aaaString = myjObject2.getString ("aaa"); System. out. println ("aaaString =" + aaaString); System. out. println ("numString =" + numString); System. out. println ("englishScore =" + englishScore); System. out. println ("historyScore =" + historyScore); System. out. println ("geographyScore =" + geographyScore) ;}} catch (JSONException e ){}
The above content is the JSON String Conversion Methods JSONObject and JSONArray introduced by the editor. I hope to help you. If you have any questions, please leave a message. The editor will reply to you in time, thank you very much for your support for the help House website!