I have a problem i explain what i have to do i have convert json format which is given below:
[
{
"Latitude": "-7.00786",
"Longitude": "34.99805",
"UserID": 0,
"HTMLCode": "<p><h3>Jhony Jhony</h3><br/><i>t would be title, info on the missionary and then links for any social links they have. So in backend if they DO NOT have a twitter account then that icon would disappear. It only shows the icons if it exists and they put in the info in the administrator backend.\r\nFor "Get Their Updates". It will ask for their email address and then email them a predetermined .pdf (which is set in admnistrator backend for that missionary). Als</i><br/><i>Tanzania</i><br/><img src=/Content/SocialMediaImages/_facebook_btrix.png/><div id=div0 style=display:none><input type=text id=txt0 /><input type=button value=Submit class=btnGetUpdatesUsingEmail data-attr-id=#txt0 data-attr-UserId=0 /> </div><a href=0 class=GetUpdates>GetUpdates</a><p>"
},
{
"Latitude": "46.91814",
"Longitude": "25.62973",
"UserID": 0,
"HTMLCode": "<p><h3>Testing</h3><br/><i>Its a testing purposes only......</i><br/><i>Romania</i><br/><div id=div0 style=display:none><input type=text id=txt0 /><input type=button value=Submit class=btnGetUpdatesUsingEmail data-attr-id=#txt0 data-attr-UserId=0 /> </div><a href=0 class=GetUpdates>GetUpdates</a><p>"
},
{
"Latitude": "29.54032",
"Longitude": "83.51368",
"UserID": 0,
"HTMLCode": "<p><h3>Nayeem Mansoori</h3><br/><i>Description for testing</i><br/><i>China</i><br/><img src=/Content/SocialMediaImages/_facebook_btrix.png/><img src=/Content/SocialMediaImages/_googleplus_btrix.png/><div id=div0 style=display:none><input type=text id=txt0 /><input type=button value=Submit class=btnGetUpdatesUsingEmail data-attr-id=#txt0 data-attr-UserId=0 /> </div><a href=0 class=GetUpdates>GetUpdates</a><p>"
}
]
And i wanna change this format to array like:
var locations = [[-7.00786, 34.99805, 0, '<p><h3>Jhony Jhony</h3><br/><i>t would be title, info on the missionary and then links for any social links they have. So in backend if they DO NOT have a twitter account then that icon would disappear. It only shows the icons if it exists and they put in the info in the administrator backend.For "Get Their Updates". It will ask for their email address and then email them a predetermined .pdf (which is set in admnistrator backend for that missionary). Als</i><br/><i>Tanzania</i><br/><img src=/Content/SocialMediaImages/_facebook_btrix.png/><div id=div0 style=display:none><input type=text id=txt0 /><input type=button value=Submit class=btnGetUpdatesUsingEmail data-attr-id=#txt0 data-attr-UserId=0 /> </div><a href=0 class=GetUpdates>GetUpdates</a><p>'], [46.91814, 25.62973, 0, '<p><h3>Testing</h3><br/><i>Its a testing purposes only......</i><br/><i>Romania</i><br/><div id=div0 style=display:none><input type=text id=txt0 /><input type=button value=Submit class=btnGetUpdatesUsingEmail data-attr-id=#txt0 data-attr-UserId=0 /> </div><a href=0 class=GetUpdates>GetUpdates</a><p>'], [29.54032, 83.51368, 0, '<p><h3>Nayeem Mansoori</h3><br/><i>Description for testing</i><br/><i>China</i><br/><img src=/Content/SocialMediaImages/_facebook_btrix.png/><img src=/Content/SocialMediaImages/_googleplus_btrix.png/><div id=div0 style=display:none><input type=text id=txt0 /><input type=button value=Submit class=btnGetUpdatesUsingEmail data-attr-id=#txt0 data-attr-UserId=0 /> </div><a href=0 class=GetUpdates>GetUpdates</a><p>']];
please help me i was googling too but not any results..
3 Answers 3
You can access the object values by their keys and add them to an array of arrays.
var locations = [[
json.Latitude,
json.Longitude,
json.UserID,
json.HTMLCode
]];
Note that you need to do this rather than using something like for..in
because JavaScript objects are not ordinal, and arrays are.
EDIT: My original answer was made before the array of objects was presented. You can iterate over the outer array normally since it is still an array.
var locations = [];
jsonArray.forEach(function (json) {
locations.push([
// see above
]);
});
1 Comment
var array = []
$.each(json, function(index, value){
var child =[];
$.each(value,function(key,value){
child.push(value);
});
array.push(child);
});
3 Comments
json
. This means you can get a different array order for two analogous JSON blobs.Here's a simple way using map()
:
var locations = $.map(json, function(v,i){
return [[v.Latitude, v.Longitude, v.UserID, v.HTMLCode]];
});
locations[0].HTMLCode
is a lot more readable thanlocations[0][3]
..apply
. Then he would need an array with the correct argument order. I can't think of another case, though, and if that's not the case here then I'm not sure why you would want to do the conversion.