简体   繁体   English

如何使用node.js从文本解析JSON文件?

[英]How to parse JSON files from text with node.js?

So I"m trying to parse out an html response into JSON for accessible objects. 因此,我正在尝试将html响应解析为JSON以访问可访问对象。

This is my router 这是我的路由器

router.get('/yammer', function(req, res) {
  var userFields;
  var yammerCode = req.query.code;
  var getYammerFieldsAddress = "http://www.yammer.combalwh;eoiahweg";
  getYammerFieldsAddress += yammerCode;
  console.log(getYammerFieldsAddress);
  httpreq.get(getYammerFieldsAddress, function(err, response) {
    if (err) return console.log(err);
    console.log(response);
    var yammerUserInfo = response.body;


    var blah = yammerUserInfo.user;
    console.log(blah);

But the info comes like this 但是信息是这样的

{
  "user":
  {
    "timezone": "Hawaii",
    "interests": null,
    "type": "user",
    "mugshot_url": "https://www.yammer.com/yamage-backstage/photos/…",
    "kids_names": null,
    "settings": {
      "xdr_proxy": "https://stagexdrproxy.yammer.com"
    },
    "schools": [],
    "verified_admin": "false",
    "birth_date": "",
    "expertise": null,
    "job_title": "",
    "state": "active",
    "contact": {
      "phone_numbers": [],
      "im": {
        "provider": "",
        "username": ""
      },
      "email_addresses": [
        {
          "type": "primary",
          "address": "test@yammer-inc.com"
        }
      ]
    },
    "location": null,
    "previous_companies": [],
    "hire_date": null,
    "admin": "false",
    "full_name": "TestAccount",
    "network_id": 155465488,
    "stats": {
      "updates": 2,
      "followers": 0,
      "following": 0
    },
    "can_broadcast": "false",
    "summary": null,
    "external_urls": [],
    "name": "clientappstest",
    "network_domains": [
      "yammer-inc.com"
    ],
    "network_name": "Yammer",
    "significant_other": null,
    "id": 1014216,
    "web_url": "https://www.yammer.com/yammer-inc.com/users/…",
    "url": "https://www.yammer.com/api/v1/users/101416",
    "guid": null
  },
  "access_token": {
    "view_subscriptions": true,
    "expires_at": null,
    "authorized_at": "2011/04/06 16:25:46 +0000",
    "modify_subscriptions": true,
    "modify_messages": true,
    "network_permalink": "yammer-inc.com",
    "view_members": true,
    "view_tags": true,
    "network_id": 155465488,
    "user_id": 1014216,
    "view_groups": true,
    "token": "ajsdfiasd7f6asdf8o",
    "network_name": "Yammer",
    "view_messages": true,
    "created_at": "2011/04/06 16:25:46 +0000"
  },

So it seems there are multiple objects coming through. 因此,似乎有多个对象通过。 I've tried accessing them from the response body, I've also tried JSON.stringify() and I can't access it. 我尝试从响应正文访问它们,也尝试了JSON.stringify(),但无法访问它。 ANy ideas? 有任何想法吗? Thanks! 谢谢!

尝试

var jsonObject = JSON.parse(response.body);

Replace this line 替换此行

var yammerUserInfo = response.body;

With

var yammerUserInfo = JSON.parse(response.body);

and it should work properly. 它应该可以正常工作。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM