Json definition and jquery json method, json definition jquery operation

Source: Internet
Author: User

Json definition and jquery json method, json definition jquery operation

I. background

Json is a lightweight data exchange format that facilitates interaction between java services and js. This article will introduce the simple definition of json and how js parses json.

Ii. Content

1. json definition:

The simple json format is [{"key1": "value1" },{ "key2": "value2"}],

[] Indicates an array, and {} indicates the data object in the array. key1 and key2 are keys in a json object. The key values in a json object are unique. value1, value2, is the value corresponding to the key.

Definition method:

1) directly spell the json String, for example, the String variable. The content is [{"attchName": "attachment 0", "attchId": 0 },{ "attchName ": "Appendix 1", "attchId": 1 },{ "attchName": "Appendix 2", "attchId": 2}].

2) introduce the json-lib.jar open source jar package, define the JSONObject object, for example:

JSONArray jsonArray = newJSONArray (); JSONObject attchJson = newJSONObject (); attchJson. put ("attchId", "0"); attchJson. put ("attchName", "attachment 0"); jsonArray. put (attchJson );

JsonArray is a json data, which is equivalent to defining a json.

Complex json definition. The value corresponding to the key in json can also be a json array. For example, a task information is encapsulated in json. There are several attachment definitions in this task:

JSONArray taskJsonArray = newJSONArray();JSONObject taskJsonObj = newJSONObject();taskJsonObj.put("taskId",100);taskJsonObj.put("taskName", "myTask");taskJsonObj.put("attchs",jsonArray);taskJsonArray.put(jsonObj);

TaskJsonArray is the final json content,

The simple form after String concatenation is as follows:

[{"Attchs": [{"attchName": "Appendix 0", "attchId": 0 },{ "attchName": "Appendix 1", "attchId ": 1 },{ "attchName": "Appendix 2", "attchId": 2}], "taskId": 100, "taskName": "myTask"}]

2. parse json in js

General json parsing method:

var json = eval_r(jsonArray ); for(var i=0;i<json.length;i++){  alert("attchId:"+json[i].attchId+",attchName:"+json[i].attchName);}

Parse json using jquery:

$. GetJSON ("jsonTest", {showNumber: "3"}, function (data) {$. each (data, function (idx, item) {// alert (idx); if (idx <0) {returntrue; // same as countinue, returns false with break} alert ("attchId:" + item. attchId + ", taskName:" + item. attchName );});});

Iii. Summary

The json format is simple, easy to parse and generate, and is a lightweight data exchange format, which is easy to use in web development.

The above is all the content of the json definition and jquery json operation method provided by xiaobian. I hope it will be helpful to you and provide more support to the customer's home ~

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.