Parsing nested JSON in Nodejs

家住魔仙堡 提交于 2019-12-08 01:54:42

问题


I have been trying to parse nested JSON data and below is my code

var string = '{"key1": "value", "key2": "value1", "Key3": {"key31":"value 31"}}';
var obj = JSON.parse(string);
console.log(obj.key1)
console.log(obj[0]);

And this is the output

$ node try.js 
value
undefined

Why I am getting undefined for obj[0]? How to get value in this case, and also for nested key key31?

Update Now with the help from @SergeyK and others, I have modified my above code as follows

    var string = '{"key1": "value1", "key2": "value2", "key3": {"key31":"value 31"}}';

    var obj = JSON.parse(string);
    var array = Object.keys(obj)

    for (var i = 0; i < array.length; i++) {
        console.log(array[i], obj[array[i]]);
    }

And the output is as follows

    $ node try.js 
    key1 value1
    key2 value2
    key3 { key31: 'value 31' }

But for {"key31":"value 31"}, how do I access Key 'key31' and get its value 'value 31'


回答1:


You just can't access named object property by index. You can use obj[Object.keys(obj)[0]]

Edit:

As @smarx explained in the comments, this answer is not suitable for direct access to the specific property by index due to Object.keys is unordered, so it is only for cases, when you need to loop keys/values of object.

Example:

var string = '{"key1": "value", "key2": "value1", "Key3": {"key31":"value 31"}}';
var obj = JSON.parse(string);
var keysArray = Object.keys(obj);
for (var i = 0; i < keysArray.length; i++) {
   var key = keysArray[i]; // here is "name" of object property
   var value = obj[key]; // here get value "by name" as it expected with objects
   console.log(key, value);
}
// output:
// key1 value
// key2 value1
// Key3 { key31: 'value 31' }



回答2:


When you tries to access

 console.log(obj[0]);

You are actually trying to refer element at very first memory location in an array, but var string is a hash not array. Thats why you are getting undefined.




回答3:


I have no idea what you want obj[0] to do, so I can't help with that.

To get the value for key31, use obj.Key3.key31, which, when logged, should yield value 31.




回答4:


console.log(obj[0]); is giving undefined because obj is not an array. obj[0] will work only if obj is an array as we are accessing the first index element from an array.

Example :

var obj = ["val1","val2","val3"];

console.log(obj[0]); // val1

As per requirement :

var string = '{"key1": "value", "key2": "value1", "Key3": {"key31":"value 31"}}';
var obj = JSON.parse(string);
var keyArray = Object.keys(obj); // key1
console.log(obj[(keyArray[0])]); // value

working fiddle :

https://jsfiddle.net/kbwbspnk/

For nested property value you have to use . operator.

console.log(obj.Key3.key31); // value 31



回答5:


your variable (String) is not array its object in above case and you can not access using indexing like 0,1,2... ,you can access this with its key value name like key1,key2...

example: obj['key1'] $ value obj['Key3']['key31'] $ value 31




回答6:


console.log(obj[0]) will display its value only if obj is an array. For example:

var obj = ["value","value2"];
console.log(obj[0]) --> value

With an object, you need to use the key as identifier.

For nested key key31:

console.log(obj.Key3.key31) --> value 31

Hope to be helpfull



来源:https://stackoverflow.com/questions/39094162/parsing-nested-json-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!