JSON string conversion methods of JSONObject and JSONArray, jsonjsonobject

Source: Internet
Author: User

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!

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.