简体   繁体   中英

How to convert json array to json object in javascript

I have a json array like in the below image:

JSon array

I want it to be onyl json object and not array. Like when I do JSON.stringify I get this: [{"total_exec_qty":"286595","total_notional":"21820771.72","total_wt_arr_last_slp":"2.4364","total_num_ords":"1630","total_wt_ivwap_slp":"6.0969","total_wt_arr_slp":"1.7889","total_ord_qty":"576991"}]

But I only want it like this: {"total_exec_qty":"286595","total_notional":"21820771.72","total_wt_arr_last_slp":"2.4364","total_num_ords":"1630","total_wt_ivwap_slp":"6.0969","total_wt_arr_slp":"1.7889","total_ord_qty":"576991"}

How do I get that?

UPDATE: Doing this right now already:

var json_temp = JSON.stringify(json4);
    console.log("json_temp")
    console.log(json_temp);
    var json_temp1 = json_temp[0];
    console.log("json temp1");
    console.log(json_temp1);

But getting the following in console.log: Getting this problem

just reference the object like this:

var array = [{"total_exec_qty":"286595","total_notional":"21820771.72","total_wt_arr_last_slp":"2.4364","total_num_ords":"1630","total_wt_ivwap_slp":"6.0969","total_wt_arr_slp":"1.7889","total_ord_qty":"576991"}];

var object = array[0];

Alternatively you could copy the object and reassign it to the same variable like the following, note: This second method is a bit more expensive.

var data = [{"total_exec_qty":"286595","total_notional":"21820771.72","total_wt_arr_last_slp":"2.4364","total_num_ords":"1630","total_wt_ivwap_slp":"6.0969","total_wt_arr_slp":"1.7889","total_ord_qty":"576991"}];

data = JSON.parse(JSON.stringify(data[0]));

Here is a working example.

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