2014-05-01 121 views
2

我有一个问题我解释什么,我必须做我有转换其在下面给出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>']]; 

请帮助我,我是谷歌搜索过但不是任何结果..

+0

你确实有JSON,或者你有一个JavaScript数组? –

+0

我有一个JSON数据,但我想改变为像[[-7.00786,34.99805,0,'nayeem']] –

+2

阵列格式]你可以简要解释为什么?您现在拥有的对象语法可能更方便,也更易于理解。 – Blazemonger

回答

0

下面是一个使用简单的方法map()

var locations = $.map(json, function(v,i){ 
    return [[v.Latitude, v.Longitude, v.UserID, v.HTMLCode]]; 
}); 
+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 
    ]); 
}); 
+0

但它不能正常工作... –

+0

其获取ReferenceError:未定义jsonArray –

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/

+3

用地图替换每个 – Metalstorm

+0

您不能依赖于键的顺序当遍历'json'时。这意味着您可以为两个类似的JSON blob获得不同的阵列顺序。 –

+0

它的作品,但不是像[[]] –