Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 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 }]"}


But I need data in this format without the array.

[{ "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 }]

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
},
Posted
Updated 14-Sep-16 0:33am
Comments
Karthik_Mahalingam 14-Sep-16 13:45pm    
alert(data.d)
this ??

C#
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 Date = "";
                var Location = "";
                var Speed = "";



                Vehicle = o[key].Vehicle;
                Date = o[key].Date;
                Location = o[key].Location;
                Speed = o[key].Speed;

            });
 
Share this answer
 
v2
Comments
bigyan sahoo 14-Sep-16 6:56am    
I receive data from services in json array. This array name in d. there is 10000 records so i can not manually set the array. so I need to remove the array name & []. only need json type string.
JavaScript
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"] 
}

}, 
 
Share this answer
 
v2
Comments
bigyan sahoo 14-Sep-16 6:54am    
When I use var dt1 = JSON.parse(JSON.stringify(data)); I rcv the array with (\), If I am using data.d then it does not work.
P_Z 14-Sep-16 7:13am    
Can you post example of array with (\)?

Can you try var dt1 = JSON.parse(data); as data is already in JSON format
bigyan sahoo 14-Sep-16 8:10am    
try
{
connection.Open();

using (SqlCommand command = new SqlCommand("DownloadEmployee", connection))
{
command.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);

//result = new JavaScriptSerializer().Serialize(dt);
result = JsonConvert.SerializeObject(dt);
//result = ser.Serialize(srtData);

}

}
catch (Exception ex)
{

}
finally
{
connection.Close();

}
return result;
}


My Service Part. It return Json Data in correct format.

$.ajax({
type: "POST",
url: "/CommonWebService.asmx/downloadExcel",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: {},
contentType: 'application/json; charset=utf-8',

success: function (data) {
//alert(data);
var dt1 = JSON.parse(JSON.stringify(data))
alert(dt1);

},
error: function () {
////alert(data.d);
alert("xyz..");


}


My Ajax method...


{"d":"[{ \"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 }]"}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900