javascript - 无法在 Nodejs 中解析 json

标签 javascript json node.js

{
    "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 中失败了。

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

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

最佳答案

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

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

如果您正在使用修改基本对象原型(prototype)的库,您可能还需要像这样的 hasOwnProperty 检查:

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

关于javascript - 无法在 Nodejs 中解析 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17456493/

相关文章:

ruby-on-rails - 修改 CURL 请求以将图像上传到 RoR

node.js - puppeteer - 确定 reCaptcha 挑战何时变得活跃/可见

javascript - 在 Node REPL 中动态重新加载模块

javascript - Mocha 测试套件在 setInterval(...) 运行时永远不会结束

javascript - 从书签加载外部 JS?

javascript - 如何在 IE11 中运行 xPath 查询?

javascript - 尝试输出唯一数据属性的列表(jQuery/Java)

javascript - 在 href 中使用数组索引

json - 使用 angular2-highchart 绘制 JSON 数据

java - 阻止 Gson 内省(introspection)类注释