使用javascript从JSON中提取数据

时间:2012-07-21 08:50:06

标签: javascript json

我有类似的JSON。我希望从此列表中提取名称,其ID和产品标题等值。但我无法弄明白。我正在尝试“eval”。

{
    "data": [{
        "id": "3092773937557",
        "from": {
            "id": "1810306393",
            "name": "Prashant Singh"
        },
        "start_time": "2012-07-21T09:12:53+0000",
        "end_time": "2012-07-21T09:12:53+0000",
        "publish_time": "2012-07-21T09:12:53+0000",
        "application": {
            "id": "132692593533721",
            "name": "Compare Hatke"
        },
        "data": {
            "productname": "Apple iPod Nano",
            "price": 399,
            "product": {
                "id": "10151004296768984",
                "url": "http:\/\/compare.buyhatke.com\/products\/Apple-iPod-Nano",
                "type": "comparehatke:product",
                "title": "Apple iPod Nano"
            }
        },
        "likes": {
            "count": 0
        },
        "comments": {
            "count": 0
        },
        "no_feed_story": false
    }, {
        "id": "3092770217464",
        "from": {
            "id": "1810306393",
            "name": "Prashant Singh"
        },
        "start_time": "2012-07-21T09:08:53+0000",
        "end_time": "2012-07-21T09:08:53+0000",
        "publish_time": "2012-07-21T09:08:53+0000",
        "application": {
            "id": "132692593533721",
            "name": "Compare Hatke"
        },
        "data": {
            "productname": "Apple iPod Nano",
            "price": 399,
            "product": {
                "id": "10151004296768984",
                "url": "http:\/\/compare.buyhatke.com\/products\/Apple-iPod-Nano",
                "type": "comparehatke:product",
                "title": "Apple iPod Nano"
            }
        },
        "likes": {
            "count": 0
        },
        "comments": {
            "count": 0
        },
        "no_feed_story": false
    }],
    "paging": {
        "next": "https:\/\/graph.facebook.com\/me\/comparehatke:compare\/?access_token=AAAB4rubm4xkBAHRhdjVgx7JxIIvUxImIm31AMxgnqEAOQsAsgZAJjBYUfvzKc8XgxDBg3AzKN1S6QU2dnmtgj7TPcoCiih1RzrL3pLpuZAgGt8eKpq&limit=2&method=get&pretty=0&offset=2"
    }
}

2 个答案:

答案 0 :(得分:0)

data = JSON.parse(yourJSONString);

如果失败,您的JSON可能会出错。您可以使用http://jsonlint.com/查找并解决问题。 在上面的粘贴中,您错过了结束]}

答案 1 :(得分:0)

<html>
   <script style="text/javscript">
    var myObject = { "data": [{
        "id": "3092741696751",
        "from": {
        "id": "1810306393",
        "name": "Prashant Singh"
    },
     "start_time": "2012-07-21T08:40:38+0000",
     "end_time": "2012-07-21T08:40:38+0000",
     "publish_time": "2012-07-21T08:40:38+0000",
     "application": {
     "id": "132692593533721",
     "name": "Compare Hatke"
     },
     "data1": {
         "productname": "Apple iPod Nano",
         "price": 399,
         "product": {
         "id": "10151004296768984",
         "url": "http:\/\/compare.buyhatke.com\/products\/Apple-iPod-Nano",
         "type": "comparehatke:product",
         "title": "Apple iPod Nano"
      }
      },
      "likes": {
      "count": 0
      },
      "comments": {
      "count": 0
      },
      "no_feed_story": false
      } ]};
   alert(myObject.data[0].id);
   </script>
 </html>