admin管理员组

文章数量:1431497

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] } ] }

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] } ] }
Share Improve this question edited Jun 17, 2015 at 17:33 Diogo Nunes asked Jun 17, 2015 at 17:01 Diogo NunesDiogo Nunes 3291 gold badge3 silver badges16 bronze badges 3
  • there's no property called attributes at the top level and by adding ´´ to the data you are turning it in to a string – Rune FS Commented Jun 17, 2015 at 17:06
  • What do you suggest doing? I used this answer's code: stackoverflow./questions/4295782/… – Diogo Nunes Commented Jun 17, 2015 at 17:09
  • I suggest that you dont convert it to a string and then payattentuon to what the properties are called from the top level to the value you need – Rune FS Commented Jun 17, 2015 at 17:13
Add a ment  | 

1 Answer 1

Reset to default 1

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.

本文标签: javascriptAccessing field on Json Object in NodejsStack Overflow