我在JS中有以下JSON。
{
"url":"http://example.com/main.aspx",
"report_template_id":"1",
"interval_secs":"86400",
"analysis_type":"lite",
"email":"[email protected]",
"alerts":["num_domains", "med_load_time", "avg_load_time", "tot_req"]
}
我希望删除警报列表,并以逗号分隔的值代替,如下所示。
{
"url":"http://example.com/main.aspx",
"report_template_id":"1",
"interval_secs":"86400",
"analysis_type":"lite",
"email":"[email protected]",
"alerts":"num_domains,med_load_time,avg_load_time,tot_req"
}
只需添加所有步骤:-
1)。将您的JSON放入变量中。
data = {"url":"http://example.com/main.aspx","report_template_id":"1","interval_secs":"86400","analysis_type":"lite","email":"[email protected]","alerts":["num_domains","med_load_time","avg_load_time","tot_req"]};
2)。将JSON数据解析为对象。假设JSON是一个字符串,起初typeof(data)
要弄清楚。
data = JSON.parse(data);
3)将警报列表更改为逗号分隔的值
data.alerts = data.alerts.join(',');
4)转换回字符串
data = JSON.stringify(data)
所以数据看起来像
{
"url": "http://example.com/main.aspx",
"report_template_id": "1",
"interval_secs": "86400",
"analysis_type": "lite",
"email": "[email protected]",
"alerts": "num_domains,med_load_time,avg_load_time,tot_req"
}
注意:-如果您只是说join()
那么,它也将起作用,因为,
仅默认分隔符是为了澄清我所给出的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句