-1

I need help to convert json to javascript objectallow me to access这样 "json.data.audience.infomation"_get the values in the json arrays?

因为我想用里面的值来做“if else 比较”

这是我的 json 结构:

{
            "data": [
                     {
                         "id": "/en/chelsea_fc",
                         "topic": "Chelsea F.C.",
                         "audience": [
                             {
                                 "userid": "100003914111287",
                                 "information": [
                                     {
                                         "category": "Athlete",
                                         "source": "Didier Drogba"
                                     },
                                     {
                                         "category": "Athlete",
                                         "source": "Frank Lampard"
                                     },
                                     {
                                         "category": "Professional sports team",
                                         "source": "Chelsea Football Club"
                                     },
                                     {
                                         "category": "favorite_teams",
                                         "source": "Chelsea Football Club"
                                     }
                                 ]
                             },
                             {
                                 "userid": "100003914111287",
                                 "information": [
                                     {
                                         "category": "Athlete",
                                         "source": "Didier Drogba"
                                     },
                                     {
                                         "category": "Athlete",
                                         "source": "Frank Lampard"
                                     },
                                     {
                                         "category": "Professional sports team",
                                         "source": "Chelsea Football Club"
                                     },
                                     {
                                         "category": "favorite_teams",
                                         "source": "Chelsea Football Club"
                                     }
                                 ]
                             }
                         ],
                         "type": "/soccer/football_team"
                     },
                     {
                         "id": "/en/manchester_united_fc",
                         "topic": "Manchester United F.C.",
                         "audience": [
                             {
                                 "information": [
                                     {
                                         "category": "Athlete",
                                         "source": "Ryan Giggs"
                                     },
                                     {
                                         "category": "Professional sports team",
                                         "source": "Manchester United"
                                     },
                                     {
                                         "category": "favorite_teams",
                                         "source": "Manchester United"
                                     }
                                 ],
                                 "userid": "100003921730958"
                             }
                         ],
                         "type": "/soccer/football_team"
                     }
                 ]
             }
4

4 回答 4

1

你可以得到你想要的

json.data[0].audience.infomation

请注意,另一个对象被包装在一个数组中。

这是一个JavaScript对象,而不是Java对象。

于 2013-01-17T06:18:32.203 回答
1

使用 jquery 库的 $.parseJSON。解析后返回一个 JQuery 对象

http://api.jquery.com/jQuery.parseJSON/

于 2013-01-17T06:18:53.270 回答
0

要将 JSON 转换为 JavaScript,只需对其进行“评估”。我建议阅读这个简短的教程。

于 2013-01-17T06:18:40.007 回答
0

您的 json 结构中有数组,因此您必须像这样访问它们:

 console.log(jsn.data[0].audience[0].information[0].category);

 output is: **Athlete**

这将log the category

这是first array item of information

这是first array item of audience

这是first array item of data

小提琴:http: //jsfiddle.net/JAUDn/

于 2013-01-17T06:49:36.333 回答