Json object and json string

Source: Internet
Author: User

This week, for work reasons, I began to contact json, a language mainly used for data exchange. In general, the format of json is simpler than xml, at least it seems that there are few rules. xml is often used to create rules due to scalability, so it is quite troublesome. Different from json, the key-value pairs, arrays, and conversion of Simple objects and strings make it very suitable for data exchange. 1. There are two structures defined in JSON: object and array 1. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs. The name is enclosed in quotation marks. If the value is a string, it must be enclosed in parentheses, but not numeric. For example: var object = {"name": "lxy", "age": 22, "birthday": "19910102"}; 2. an array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. Values are separated by commas. Ii. Conversion 1. json String -- JSONObject object -- String jsonMessage = "{\" \ ": \" 88 \ ", \" \": \ "78 \", \ "computer \": \ "99 \"} "; String value1 = null; try {// convert the string to jsonObject JSONObject myJsonObject = new JSONObject (jsonMessage); // obtain the value value1 = myJsonObject. getString ("Mathematics");} catch (JSONException e) {} 2. java object -- JSONObject object -- json string User user = new User (); user. setName ("lxy"); user. setSex (22); JSONObject myJsonObject = JSONObject. fromObject (user); System. out. println (jo2.toString (); var jsonArrStr = [{'name': 'zhang san', 'age': '20'}, {'name': 'Li si ', 'age': '18'}];

Related Article

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.