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',
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745541922a4632165.html
评论列表(0条)