初次使用json;
2007-04-18 14:40 迷路中的路人甲 阅读(285) 评论(0) 编辑 收藏 举报
第一次使用JSON,感觉还比XML方便一点,只是在最后赋值的时候出了点问题;
str = re.responseText;
info = eval('('+str+')') ; 赋值的时候要多加一个括号,找到半天,还以为自己的格式搞错了;
由于JSON是在js基础上的,可以通过设置默认值的方式,进行显示,如果一步不到数据也可以显示出默认数据,不像XML如果没有数据返回来,就无法显示了,或许xml也可以设置默认数据,只是我还不知道吧;
正确的JSON格式
str = re.responseText;
info = eval('('+str+')') ; 赋值的时候要多加一个括号,找到半天,还以为自己的格式搞错了;
由于JSON是在js基础上的,可以通过设置默认值的方式,进行显示,如果一步不到数据也可以显示出默认数据,不像XML如果没有数据返回来,就无法显示了,或许xml也可以设置默认数据,只是我还不知道吧;
正确的JSON格式
1 var people =
2 { "programmers": [
3 { "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
4 { "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" },
5 { "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" }
6 ],
7 "authors": [
8 { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
9 { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
10 { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
11 ],
12 "musicians": [
13 { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
14 { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
15 ]
16 }
17
2 { "programmers": [
3 { "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
4 { "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" },
5 { "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" }
6 ],
7 "authors": [
8 { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
9 { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
10 { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
11 ],
12 "musicians": [
13 { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
14 { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
15 ]
16 }
17