如何将JSON数组转换为json数据。 [英] How do I convert a JSON array to json data.
问题描述
她是我的代码
我收到这种格式的数据。
{d:[{Vehicle:BMW,Date:30,Jul 2013 09:24 AM,Location:Hauz Khas,Enclave,New Delhi,Delhi,印度,速度:42},
{车辆:本田CBR,日期:30,2013年7月12日上午12:00,位置:军事道路,西孟加拉邦734013,印度,速度:0},
{车辆:本田雅阁,日期:2013年7月30日上午11:05,地点:DLF Phase IV,Super Mart 1,Gurgaon,Haryana,India,Speed:71}]}
但我需要这种格式的数据没有数组。
[{车辆:宝马,日期:30,2013年7月09:24 AM,位置:Hauz Khas,Enclave,新德里,德里,印度,速度:42},
{车辆:本田CBR,日期: 2013年7月30日上午12:00,地点:军事道路,西孟加拉邦734013,印度,速度:0},
{车辆:本田雅阁 ,日期:2013年7月30日上午11:05,Loca :DLF第四期,超级市场1,古尔冈,哈里亚纳邦,印度,速度:71}]
我尝试过:
成功:功能(数据){
var dt = JSON.stringify(数据);
var newdt = dt.replace(/ \\ / g,);
alert(newdt); //这里我得到了这个Json数组,我需要将这种格式改为json
},
Her is my code
I receive data in this format.
{"d":"[{ "Vehicle": "BMW", "Date": "30, Jul 2013 09:24 AM", "Location": "Hauz Khas, Enclave, New Del Del India", "Speed": 42 },
{ "Vehicle": "Honda CBR", "Date": "30, Jul 2013 12:00 AM", "Location": "Military Road, West Bengal 734013, India", "Speed": 0 },
{ "Vehicle": "Honda Accord", "Date": "30, Jul 2013 11:05 AM", "Location": "DLF Phase IV, Super Mart 1, Gurgaon, Haryana, India", "Speed": 71 }]"}
But I need data in this format without the array.
[{ "Vehicle": "BMW", "Date": "30, Jul 2013 09:24 AM", "Location": "Hauz Khas, Enclave, New Del Del India", "Speed": 42 },
{ "Vehicle": "Honda CBR", "Date": "30, Jul 2013 12:00 AM", "Location": "Military Road, West Bengal 734013, India", "Speed": 0 },
{ "Vehicle": "Honda Accord", "Date": "30, Jul 2013 11:05 AM", "Location": "DLF Phase IV, Super Mart 1, Gurgaon, Haryana, India", "Speed": 71 }]
What I have tried:
success: function (data) {
var dt = JSON.stringify(data);
var newdt = dt.replace(/\\/g, "");
alert(newdt); //Here i get this Json array and I need to chenge this format to json
},
推荐答案
var o = [{ "Vehicle": "BMW", "Date": "30, Jul 2013 09:24 AM", "Location": "Hauz Khas, Enclave, New Delhi, Delhi, India", "Speed": 42 },
{ "Vehicle": "Honda CBR", "Date": "30, Jul 2013 12:00 AM", "Location": "Military Road, West Bengal 734013, India", "Speed": 0 },
{ "Vehicle": "Honda Accord", "Date": "30, Jul 2013 11:05 AM", "Location": "DLF Phase IV, Super Mart 1, Gurgaon, Haryana, India", "Speed": 71 }]
。 each(o,function(key,val){
var Vehicle = 跨度>;
var 日期= ;
var 位置= ;
var Speed = ;
Vehicle = o [key] .Vehicle;
Date = o [key] .Date;
Location = o [key] .Location;
Speed = o [key] .Speed;
});
.each(o, function (key, val) { var Vehicle = ""; var Date = ""; var Location = ""; var Speed = ""; Vehicle = o[key].Vehicle; Date = o[key].Date; Location = o[key].Location; Speed = o[key].Speed; });
success: function (data) {
var dt = JSON.stringify(data);
var newdt = dt.replace(/\\/g, "");
alert(newdt); //Here i get this Json array and I need to chenge this format to json
Can you try code such as:
var dt1 = JSON.parse(JSON.stringify(data.d)); //Edit: added .d which the is root element of the data
//Iterate over records
for (var i = 0; i < dt1.length; i++) {
var row = dt1[i];
var vehicle = row.Vehicle; //or you can get values such as = row["Vehicle"]
}
},
这篇关于如何将JSON数组转换为json数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!