1
{
    "status": 1,
    "complete": 1,
    "list": {
        "378144457": {
            "item_id": "378144457",
            "resolved_id": "378144457",
            "given_url": "https://ngrok.com",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1372274736",
            "time_updated": "1372274741",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 3,
            "resolved_title": "Introspected tunnels to localhost",
            "resolved_url": "https://ngrok.com",
            "excerpt": "ngrok creates a tunnel from the public internet (http://subdomain.ngrok.com) to a port on your local machine. You can give this URL to anyone to allow them to try out a web site you're developing without doing any deployment.  ngrok captures all traffic through the tunnel.",
            "is_article": "0",
            "is_index": "1",
            "has_video": "0",
            "has_image": "1",
            "word_count": "422"
        },
        "380099389": {
            "item_id": "380099389",
            "resolved_id": "380099389",
            "given_url": "http://physics.aps.org/articles/v6/69",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1371518628",
            "time_updated": "1371518632",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 5,
            "resolved_title": "Viewpoint: New Particle Hints at Four-Quark Matter",
            "resolved_url": "http://physics.aps.org/articles/v6/69",
            "excerpt": "Particle physicists seem to have a pretty good handle on the fundamental particles of the universe, but there are some glaring holes in this understanding. Quarks are a good example of this.",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "1",
            "word_count": "1053"
        },
        "385567551": {
            "item_id": "385567551",
            "resolved_id": "385567551",
            "given_url": "http://gtrak.wordpress.com/2013/06/26/clojure-tradeoffs/",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1372794240",
            "time_updated": "1372794247",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 0,
            "resolved_title": "Clojure Tradeoffs (design implications and why you should care)",
            "resolved_url": "http://gtrak.wordpress.com/2013/06/26/clojure-tradeoffs/",
            "excerpt": "Clojure as a language and community is very sensitive to the definition and design of tradeoffs. This post is an attempt to elucidate the tradeoffs chosen by the language, what they mean to interested parties, and an attempt to predict the future based on these choices.",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "0",
            "word_count": "2568"
        },
        "385823467": {
            "item_id": "385823467",
            "resolved_id": "385823467",
            "given_url": "http://devo.ps/blog/2013/06/26/goodbye-node-forever-hello-pm2.html",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1372275849",
            "time_updated": "1372275853",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 2,
            "resolved_title": "Goodbye node-forever, hello PM2",
            "resolved_url": "http://devo.ps/blog/2013/06/26/goodbye-node-forever-hello-pm2.html",
            "excerpt": "It’s no secret that the devo.ps team has a crush on Javascript; node.js in the backend, AngularJS for our clients, there isn’t much of our stack that isn’t at least in part built with it. Our approach of building static clients and RESTful JSON APIs means that we run a lot of node.",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "1",
            "word_count": "800"
        },
        "386086417": {
            "item_id": "386086417",
            "resolved_id": "386086417",
            "given_url": "https://news.ycombinator.com/item?id=5946900",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1372274365",
            "time_updated": "1372274367",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 4,
            "resolved_title": "Richard Stallman Inducted Into the 2013 Internet Hall of Fame",
            "resolved_url": "https://news.ycombinator.com/item?id=5946900",
            "excerpt": "The man never misses an opportunity to try and get people thinking about the issues that matter.  I'm unconvinced. The majority of people are sharing more about themselves online. Not less.",
            "is_article": "0",
            "is_index": "0",
            "has_video": "0",
            "has_image": "0",
            "word_count": "2316"
        },
        "388862014": {
            "item_id": "388862014",
            "resolved_id": "388862014",
            "given_url": "http://imgur.com/a/Op82P",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1372692033",
            "time_updated": "1372692040",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 1,
            "resolved_title": "Upload images",
            "resolved_url": "http://imgur.com/a/Op82P",
            "excerpt": "",
            "is_article": "0",
            "is_index": "0",
            "has_video": "0",
            "has_image": "2",
            "word_count": "0"
        }
    },
    "since": 1372879049
}

我正在尝试用 Javascript 解析这个 json,但在 node.js 中失败了。

 data = JSON.parse(body); //body is the json response
 dat = data["list"];

我从这里得到列表,但我该如何解析呢?JSON.stringify(dat) 也不起作用。

4

1 回答 1

2

您需要使用这样的for in循环:

for (var id in data["list"]){
    console.log(id + " has given_url " +data["list"][id]["given_url"]);
}

如果您正在使用修改基本 Object 原型的库,您可能还需要这样的hasOwnProperty检查:

for (var id in data["list"]){
    if (data["list"].hasOwnProperty(id)){
        console.log(id + " has given_url " +data["list"][id]["given_url"]);
    }
}
于 2013-07-03T19:43:08.360 回答