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