admin管理员组

文章数量:1279145

I'm having trouble reading properties from JSON within NodeJS.

Feels like an obvious mistake I may be making..

The JSON is from this endpoint;

.js

My code:

var request = require('request');

request(".js", function (err, res, json) {
  JSON.parse(json);
  console.log(json["1"]["artist"]);  // undefined
});

~

I'm having trouble reading properties from JSON within NodeJS.

Feels like an obvious mistake I may be making..

The JSON is from this endpoint;

http://hypem./playlist/history/faisdotal/json/1/data.js

My code:

var request = require('request');

request("http://hypem./playlist/history/faisdotal/json/1/data.js", function (err, res, json) {
  JSON.parse(json);
  console.log(json["1"]["artist"]);  // undefined
});

~

Share Improve this question asked Jan 24, 2012 at 0:22 faiizowfaiizow 851 gold badge1 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You need to store the returned value of JSON.parse:

json = JSON.parse(json);
console.log(json["1"]["artist"]);

I think you want:

json = JSON.parse(json);

It won't (and can't) simply update the value of the parameter. The .parse() routine returns the value parsed from the string you pass it.

JavaScript is purely call-by-value, so there's really no way it could possibly work the way your code is written.

本文标签: javascriptReading JSON properties in NodeJSStack Overflow