0%

js怎样读取json里面的数据


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
var json = {  
contry:{
area:{
man:"12万",
women:"10万"
}
}
};
//方式一:使用eval解析
var obj = eval(json);
alert(obj.constructor);
alert(obj.contry.area.women);

//方式二:使用Funtion函数
var strJSON = "{name:'json name'}";//得到的JSON
var obj = new Function("return" + strJSON)();//转换后的JSON对象
alert(obj.name);//json name
alert(obj.constructor);

//复杂一点的json数组数据的解析
var value1 = [
{
"a01":"1",
"a02":"2",
"a03":"3",
"a04":"4",
},
{
"b01":"2",
"b02":"4",
"b03":"5",
"b04":"2",
},
{
"c01":"5",
"c02":"1",
"c03":"4",
"c04":"11",
}
];
var obj1 = eval(value1);
alert(obj1[0].c01);

//复杂一点的json的另一种形式
var value2 = {
"list":[
{
"password":"1230",
"username":"coolcooldool"
},
{
"password":"thisis2",
"username":"okokok"
}
],
"array":[
{
"password":"1230",
"username":"coolcooldool"
},
{
"password":"thisis2",
"username":"okokok"
}
]
};
var obj2 = eval(value2);
alert(obj2.list[0].password);
------ The End ------
您的认可是我不断进步的动力!