Json object in js, jsjson object
1. JSON (JavaScript Object Notation) is a simple data format, which is lighter than xml. JSON is a JavaScript native format, which means that no special API or toolkit is required to process JSON data in JavaScript.
JSON rules are simple: the object is an unordered set of "'name: value' pairs. 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 rules are as follows:
1) The ing is represented by a colon. Name: Value
2) The parallel data is separated by commas. Name 1: value 1, name 2: Value 2
3) The ing set (object) is represented by braces. {Name 1: value 1, name 2: Value 2}
4) The set (array) of the parallel data is represented by square brackets.
[
{Name 1: value, name 2: Value 2 },
{Name 1: value, name 2: Value 2}
]
5) Types of element values: string, number, object, array, true, false, null
2. Five methods in json:
1) traditional data storage and calling data
<Script type = "text/javascript"> // JS defines the "class" function Person (id, name, age) {this. id = id; this. name = name; this. age = age;} // create an "object" var p = new Person (20141028, "one leaf and one boat", 22) in the traditional JS mode; // call attributes in the class, display the information of this Person window. alert (p. id); window. alert (p. name); window. alert (p. age); </script>
2) first style:
<Script type = "text/javascript"> var person = {id: 001, name: "", age: 23} window. alert ("No.:" + person. id); window. alert ("username:" + person. name); window. alert ("Age:" + person. age); </script>
3) The second style:
<Script type = "text/javascript"> var p = [{id: 001, name: "", age: 22}, {id: 002, name: "No regrets", age: 23}, {id: 003, name: "No regrets _ A leaf boat", age: 24}]; for (var I = 0; I <p. length; I ++) {window. alert ("No.:" + p [I]. id); window. alert ("username:" + p [I]. name); window. alert ("Age:" + p [I]. age) ;}</script>
4) Third style:
<Script type = "text/javascript"> var p = {"province": [{"city": "Fuzhou" },{ "city": "Xiamen "}, {"city": "Putian"}]}; window. alert ("City:" + p. province [0]. city); </script>
5) Fourth style:
<Script type = "text/javascript"> var p = {"ids": [{"id": 001 },{ "id": 002 },{ "id ": 003}], "names": [{"name": "" },{ "name": "No regrets" },{ "name ": "No regrets _ one leaf fl"}]}; for (var I = 0; I <p. names. length; I ++) {window. alert ("name:" + p. names [I]. name) ;}for (var I = 0; I <p. ids. length; I ++) {window. alert ("id:" + p. ids [I]. id) ;}</script>
6) Fifth style:
<Script type = "text/javascript"> var p = {"province": ["Fuzhou", "Xiamen", "Putian"]}; window. alert ("number of cities:" + p. province. length); window. alert ("respectively: \ n"); for (var I = 0; I <p. province. length; I ++) {window. alert (p. province [I]) ;}</script>
How to retrieve the object Value in json in js
Var str = {"code": 0, "data": [{"uid": "689791", "username": "vaghyuzhihao", "orgid": "627 ", "status": "1", "subname": "yuzhihao", "roleids": "1", "orgcode": "600251", "name": "yuzhihao ", "mapid": "0", "passwd": "", "siteid": "0", "createorgcode": "600251", "alarmview ": "0"}]}; var json = eval (str); console. log (json. data [0]. uid); Generally, strings are returned and need to be objectized.
Display of json objects in js
$. Post ('jsonservlet ', {params: string}, function (data) {data = eval (data); for (var j in data) {var key = j; var value = data [j]; $ ("span [id =" + key + "]" ).html (value) ;}}); change your code to this, line