javascript - Not able to replace all occurence of a json key correctly -


hi have nested json object.

i want rename occurrences of total_exec_qty_buy & total_exec_qty_sell total_exec_qty; total_num_ords_buy & total_num_ords_sell total_num_ords

i'm doing in following way:

newarray = json.stringify(newarray).split('"total_exec_qty_buy":').join('"total_exec_qty":'); 

when 1 statement(above one) output fine. when this:

newarray = json.stringify(newarray).split('"total_exec_qty_buy":').join('"total_exec_qty":');      newarray = json.stringify(newarray).split('"total_exec_qty_sell":').join('"total_exec_qty":'); newarray = json.stringify(newarray).split('"total_num_ords_buy":').join('"total_num_ords":');     newarray = json.stringify(newarray).split('"total_num_ords_sell":').join('"total_num_ords":'); 

then output:

"\"\\\"[{\\\\\\\"total_wt_arr_slp_buy\\\\\\\":\\\\\\\"-1.9322\\\\\\\",\\\\\\\"total_exec_qty\\\\\\\":\\\\\\\"49654\\\\\\\",\\\\\\\"total_wt_buy\\\\\\\":\\\\\\\"0.31\\\\\\\",\\\\\\\"total_wt_arr_last_slp_buy\\\\\\\":\\\\\\\"-0.1924\\\\\\\",\\\\\\\"total_wt_ivwap_slp_buy\\\\\\\":\\\\\\\"-0.1103\\\\\\\",\\\\\\\"total_exec_val_buy\\\\\\\":\\\\\\\"3224372.00\\\\\\\",\\\\\\\"total_ord_qty_buy\\\\\\\":\\\\\\\"61688\\\\\\\",\\\\\\\"total_num_ords_buy\\\\\\\":\\\\\\\"859\\\\\\\"},{\\\\\\\"total_wt_arr_last_slp_sell\\\\\\\":\\\\\\\"-1.2509\\\\\\\",\\\\\\\"total_ord_qty_sell\\\\\\\":\\\\\\\"139654\\\\\\\",\\\\\\\"total_exec_qty_sell\\\\\\\":\\\\\\\"111012\\\\\\\",\\\\\\\"total_wt_ivwap_slp_sell\\\\\\\":\\\\\\\"-0.1833\\\\\\\",\\\\\\\"total_wt_sell\\\\\\\":\\\\\\\"0.69\\\\\\\",\\\\\\\"total_exec_val_sell\\\\\\\":\\\\\\\"7129344.00\\\\\\\",\\\\\\\"total_num_ords_sell\\\\\\\":\\\\\\\"1099\\\\\\\",\\\\\\\"total_wt_arr_slp_sell\\\\\\\":\\\\\\\"-2.4978\\\\\\\"},{\\\\\\\"total_wt_arr_last_slp\\\\\\\":\\\\\\\"-0.9213\\\\\\\",\\\\\\\"total_exec_qty\\\\\\\":\\\\\\\"160666\\\\\\\",\\\\\\\"total_wt_ivwap_slp\\\\\\\":\\\\\\\"-0.1606\\\\\\\",\\\\\\\"total_wt_arr_slp\\\\\\\":\\\\\\\"-2.3216\\\\\\\",\\\\\\\"total_ord_qty\\\\\\\":\\\\\\\"201342\\\\\\\",\\\\\\\"total_num_ords\\\\\\\":\\\\\\\"1958\\\\\\\",\\\\\\\"total_notional\\\\\\\":\\\\\\\"10353698.63\\\\\\\"}]\\\"\"" 

why happening??

this happening because keep calling json.stringify().

stringify json object once , use str.replace() instead:

var jsonstr = json.stringify(jsonobj); jsonstr = jsonstr.replace("total_exec_qty_buy", "total_exec_qty");     jsonstr = jsonstr.replace("total_exec_qty_sell", "total_exec_qty");  jsonstr = jsonstr.replace("total_num_ords_buy", "total_num_ords");  jsonstr = jsonstr.replace("total_num_ords_sell", "total_num_ords");  

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -