在nodejs中解析JSON [英] parsing JSON in nodejs

查看:2794
本文介绍了在nodejs中解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的json

{id:"12",data:"123556",details:{"name":"alan","age":"12"}}

我使用下面的代码进行解析

i used the code below to parse

var chunk={id:"12",data:"123556",details:{"name":"alan","age":"12"}}
var jsonobj = JSON.parse(chunk);
console.log(jsonobj.details);

我收到的输出是

{"name":"alan","age":"12"}

我需要从细节中获取单个字符串,说我应该能够解析并获取名称"的值.我被困在这里,任何帮助将不胜感激

I need to get the individual strings from details say i should be able to parse and get the value of "name".I am stuck here any help will be much appreciated

推荐答案

如果您已经有一个对象,则无需解析它.

If you already have an object, you don't need to parse it.

var chunk={id:"12",data:"123556",details:{"name":"alan","age":"12"}};
// chunk is already an object!

console.log(chunk.details);
// => {"name":"alan","age":"12"}

console.log(chunk.details.name);
//=> "alan"


仅在处理实际的json string时使用JSON.parse().例如:


You only use JSON.parse() when dealing with an actual json string. For example:

var str = '{"foo": "bar"}';
console.log(str.foo);
//=> undefined

// parse str into an object
var obj = JSON.parse(str);

console.log(obj.foo);
//=> "bar" 

有关更多详细信息,请参见 json.org

See json.org for more details

这篇关于在nodejs中解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆