简体   繁体   中英

How to convert JavaScript object containing json string

I am having problem parsing JSON string into JS object .Please tell how to convert JavaScript object :

Object {d: "[{"worker_id":1,"worker_name":"Shivank"}]"}

into

Object { d: [{ "worker_id": 1, "worker_name": "Shivank" }] } 

I have tried using

JSON.parse(data) 

and

var dataFinal = JSON.stringify(data);
var d1 = eval('(' +dataFinal+ ')');

You have an object where one property value contains JSON so you only need to convert that value

Try

data.d= JSON.parse(data.d);

Assuming your data is as below, which d is having stringified json data

var data = {d: "[{\"worker_id\":1,\"worker_name\":\"Shivank\"}]"}

console.log(data);

You can parse JSON and assign to d key

data.d = JSON.parse(data.d)

console.log(data); // required output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM