将Json数据转换为数组

时间:2014-05-01 14:39:16

标签: javascript jquery json

我有一个问题,我解释了我必须做的事情,我已经转换了json格式,如下所示:

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

我想将此格式更改为数组:

 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>']];

请帮助我,我也在谷歌搜索,但没有任何结果..

3 个答案:

答案 0 :(得分:4)

您可以通过键访问对象值,并将它们添加到数组数组中。

var locations = [[
    json.Latitude,
    json.Longitude,
    json.UserID,
    json.HTMLCode
]];

请注意,您需要执行此操作而不是使用for..in之类的内容,因为JavaScript对象不是序数,而数组是。

编辑:我的原始答案是在呈现对象数组之前做出的。您可以正常迭代外部数组,因为它仍然是一个数组。

var locations = [];
jsonArray.forEach(function (json) {
    locations.push([
        // see above
    ]);
});

答案 1 :(得分:3)

var array = []
$.each(json, function(index, value){
   var child =[];
   $.each(value,function(key,value){
      child.push(value);
  });
  array.push(child);
});

http://jsfiddle.net/jhon_sharma/fTPFS/

答案 2 :(得分:0)

这是使用map()的简单方法:

var locations = $.map(json, function(v,i){
    return [[v.Latitude, v.Longitude, v.UserID, v.HTMLCode]];
});