Atul Takekar Atul Takekar - 5 months ago 10
Javascript Question

javascript serialization without colomn name

Hi I want to retrieve latitude and longitude from database and saving it into hidden field.

Table dt retrieved from database

latitude longitude
19.2094000000 73.0939000000
19.2244070000 73.1545760000
19.1659242536 82.2436523438
18.3336694458 80.4309082031


I am using javascript serializer to save it in the hidden field

JavaScriptSerializer oSerializer = new JavaScriptSerializer();

var Result = (from c in dt.AsEnumerable()
select new
{
Latitude = c.Field<Decimal>("Latitude"),
Longitude = c.Field<Decimal>("Longitude")
}).ToList();

hdnControl.Value = oSerializer.Serialize(Result);


this results into

[
{"Latitude":19.2094000000,"Longitude":73.0939000000},
{"Latitude":19.2244070000,"Longitude":73.1545760000},
{"Latitude":19.1659242536,"Longitude":82.2436523438},
{"Latitude":18.3336694458,"Longitude":80.4309082031}
]


but I do not want column name in result data as I need to further process it and extract lat and long from it.

How can I save in hidden field without column name, so data would look like this?

[
{19.2094000000,73.0939000000},
{19.2244070000,73.1545760000},
{19.1659242536,82.2436523438},
{18.3336694458,80.4309082031}
]

Answer

You would create a new array and add the formatted content in it:

var old = [
  {"Latitude":19.2094000000,"Longitude":73.0939000000},
  {"Latitude":19.2244070000,"Longitude":73.1545760000},
  {"Latitude":19.1659242536,"Longitude":82.2436523438},
  {"Latitude":18.3336694458,"Longitude":80.4309082031}
];

var new = [];

for (i = 0; i < old.length; i++) { 
    new.push({old[i].Latitude, old[i].Longitude});
}

"old" is your array you've received from the database while "new" is a new created array.

Than you loop through all items of the old array and add only the value (without the key) of your old array's item.

at the end "new" contains exactly what you asked for.