admin管理员组文章数量:1429993
I am sending the string from AngularJS to NodeJS in following form.
"{↵obj:{↵one:string,↵two:integer↵}↵}"//request object from browser console
I want to convert this string in object and use its properties.for this the server side code is following:
var data=req.body.data.replace(/"/g,"");
var str=data.replace(/\n/g," ");
//res.json(str) // returning "{ obj:{ one:string, two:integer } }"
try {
var obj=JSON.parse(JSON.stringify(str)).replace(/"/g,"");
res.json(obj);//returning same "{ obj:{ one:string, two:integer } }"
} catch (error) {
console.log(error);
}
I want to get ['users'] by Object.keys[obj] or by any other method want to access the properties of this object.but failed to achieve this.
Many solutions like This couldn't be helpful.any suggestion here??
I am sending the string from AngularJS to NodeJS in following form.
"{↵obj:{↵one:string,↵two:integer↵}↵}"//request object from browser console
I want to convert this string in object and use its properties.for this the server side code is following:
var data=req.body.data.replace(/"/g,"");
var str=data.replace(/\n/g," ");
//res.json(str) // returning "{ obj:{ one:string, two:integer } }"
try {
var obj=JSON.parse(JSON.stringify(str)).replace(/"/g,"");
res.json(obj);//returning same "{ obj:{ one:string, two:integer } }"
} catch (error) {
console.log(error);
}
I want to get ['users'] by Object.keys[obj] or by any other method want to access the properties of this object.but failed to achieve this.
Many solutions like This couldn't be helpful.any suggestion here??
Share Improve this question edited Nov 23, 2016 at 7:31 N.A asked Nov 23, 2016 at 7:23 N.AN.A 8555 gold badges13 silver badges34 bronze badges 4-
2
Why are you removing quotes and newlines? If it's valid JSON then all you need to do is call
JSON.parse()
onreq.body.data
. – mscdex Commented Nov 23, 2016 at 7:32 - It seems that this is not a valid JSON – ThomasThiebaud Commented Nov 23, 2016 at 7:37
- it is not parsing req.body.data directly.by doing this JSON.parse(JSON.stringify(req.body.data)) the answer is "{\nobj:{\none:string,\ntwo:integer\n}\n}".....still properties are not accessible – N.A Commented Nov 23, 2016 at 7:39
- @ThomasThiebaud error after json.parse shows this but how can i validate this json in case.???can u please explain? – N.A Commented Nov 23, 2016 at 7:42
2 Answers
Reset to default 2All you need is JSON.parse
to convert string to object.
Something like this:
var jsonString='{"obj":{"one":"string","two":"integer"}}';
console.log(JSON.parse(jsonString));
Output:
{ obj: { one: 'string', two: 'integer' } }
For example my raw data is like this :
var raw_data =
{
"ok": true,
"user": {
"id": "U89MZ4PV2",
"team_id": "T895HCY8H",
"name": "hyosoka187",
}
}
then just use JSON.parse(raw_data, true);
. So
var real_data = JSON.parse(raw_data, true);
console.log(real_data.user.name);
Result :
hyosoka187
NOTE :
Output of JSON.parse(raw_data, true) :
{
ok: true,
user: {
id: 'U89MZ4PV2',
team_id: 'T895HCY8H',
name: 'hyosoka187',
}
}
本文标签: javascriptMake an object from a string in node jsStack Overflow
版权声明:本文标题:javascript - Make an object from a string in node js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745541922a2662531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论