如何在javascript中将json数组转换为json对象

时间:2016-10-28 15:53:10

标签: javascript json

我有一个json数组,如下图所示:

JSon array

我希望它是onyl json对象而不是数组。就像我做JSON.stringify一样,我得到了这个:[{"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"}]

但我只想这样:{"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 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);

但是在console.log中获得以下内容: Getting this problem

1 个答案:

答案 0 :(得分:2)

只需引用这样的对象:

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];

或者你可以复制对象并将其重新分配给同一个变量,如下所示,注意:第二种方法有点贵。

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是一个有效的例子。