简体   繁体   中英

Accessing field on Json Object in Node.js

So I'm new to JavaScript programming. I'm getting this JSON payload on a POST request:

{
  "subscriptionId" : "asdasdasdasd",
  "originator" : "localhost",
  "contextResponses" : [
    {
      "contextElement" : {
        "type" : "",
        "isPattern" : "false",
        "id" : "id",
        "attributes" : [
          {
            "name" : "temperature",
            "type" : "int",
            "value" : "5"
          }
        ]
      },
      "statusCode" : {
        "code" : "200",
        "reasonPhrase" : "OK"
      }
    }
  ]
}

And using this code to try and access a certain field:

var qs = require('querystring');
var http = require('http');
http.createServer(function (req, res) {
var obj;

if (req.method == 'POST') {
        var body = '';
        req.on('data', function (data) {
            body += data;
            console.log(body.attributes.value);
            if (body.length > 1e6)
                req.connection.destroy();
        });

        req.on('end', function () {
            var post = qs.parse(body);
            // use post['blah'], etc.
        });
    }
}).listen(8087, "188.???.??.???");
console.log('Server running at http://188.???.??.???:8087/');

As you can see I'm trying to access the value field and trying to retrieve the number 5. Obviously, it's not working. I tried googleing it and this is probably something really silly. Any suggestions on how to access the field?

EDIT:

The data from console.log(body):

Server running at http://????????
{
  "subscriptionId" : "asdasdasdasd",
  "originator" : "localhost",
  "contextResponses" : [
    {
      "contextElement" : {
        "type" : "",
        "isPattern" : "false",
        "id" : "fiwaresensorfinal",
        "attributes" : [
          {
            "name" : "temperature",
            "type" : "int",
            "value" : "5"
          }
        ]
      },
      "statusCode" : {
        "code" : "200",
        "reasonPhrase" : "OK"
      }
    }
  ]
}

The console.log(data):

<Buffer 7b 0a 20 20 22 73 75 62 73 63 72 69 70 74 69 6f 6e 49 64 22 20 3a 20 22 35 35 38 31 37 62 33 62 39 38 61 64 64 31 38 63 63 33 65 31 38 33 62 65 22 2c 0a ...>

The console.log(recv):

{ subscriptionId: '55817b3b98add18cc3e183be',
  originator: 'localhost',
  contextResponses: [ { contextElement: [Object], statusCode: [Object] } ] }

Since data is returned as a string by http module, you need to parse your received data to JSON object first:

Instead of :

req.on('data', function (data) {
        body += data;
        console.log(body.attributes.value);
        ....
});

Do this:

req.on('data', function (data) {
        var recv = JSON.parse(data);
        console.log(recv.contextResponses[0].contextElement.attributes[0].value);
        ....
});

If your data received as a response from the server is as you posted in the first paragraph, then you can access the value via .contextResponses[0].contextElement.attributes[0].value .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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