怎么把 json 格式转换成 数组格式?
发布网友
发布时间:2022-04-23 01:59
我来回答
共4个回答
热心网友
时间:2022-04-29 08:13
var obj = {"pieTes":[{"colors":"Sony","value":7},{"colors":"Samsumg","value":14},{"colors":"LG","value":21},{"colors":"Vizio","value":34},{"colors":"Insignia","value":24}]};
var arr = new Array();
for(var i in x.pieTes) {
arr.push([x.pieTes[i].colors, x.pieTes[i].value]);
}
alert(arr);
// 输出 Sony,7,Samsung,14,LG,21,Vizio,34,Insignia,24
alert 不会以数组形式显示,实际结果应该如图所示 console.log(arr);
热心网友
时间:2022-04-29 09:31
php直接json_decode($json,true) 这样就可以得到数组的
js的话 var array = eval(json),然后将array循环,就可以得到一个数组
热心网友
时间:2022-04-29 11:06
java版——org_jsonParser2_JsonStr_Arr.java
package com.ljx.neln.parser;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class org_jsonParser2_JsonStr_Arr {
//jsonObjStr_complex
private static final String COMPLEX_JSON_STR = "{\"pieTes\":[{\"colors\":\"Sony\",\"value\":7},{\"colors\":\"Samsumg\",\"value\":14},{\"colors\":\"LG\",\"value\":21},{\"colors\":\"Vizio\",\"value\":34},{\"colors\":\"Insignia\",\"value\":24}]}";
public static void main(String[] args) {
ComplexJSONStr_Array();
}
/**
* JOSN格式:{"pieTes":[{"colors":"Sony","value":7},{"colors":"Samsumg","value":14},{"colors":"LG","value":21},{"colors":"Vizio","value":34},{"colors":"Insignia","value":24}]}
用JS 转换成 数组 [['Sony',7], ['Samsumg',14], ['LG',21], ['Vizio',34], ['Insignia', 24]];
*/
public static void ComplexJSONStr_Array(){
try {
//备注:COMPLEX_JSON_STR="{"pieTes":[{"colors":"Sony","value":7},{"colors":"Samsumg","value":14},{"colors":"LG","value":21},{"colors":"Vizio","value":34},{"colors":"Insignia","value":24}]}"
//jsonObjstr转换成jsonObj
JSONObject jsonObject=new JSONObject(COMPLEX_JSON_STR);//{"pieTes":[{"colors":"Sony","value":7},{"colors":"Samsumg","value":14},{"colors":"LG","value":21},{"colors":"Vizio","value":34},{"colors":"Insignia","value":24}]}
//jsonObj根据key-value,得到jsonArray
JSONArray jsonArrayOut=jsonObject.getJSONArray("pieTes");//[{"colors":"Sony","value":7},{"colors":"Samsumg","value":14},{"colors":"LG","value":21},{"colors":"Vizio","value":34},{"colors":"Insignia","value":24}]
Object[] array=new Object[jsonArrayOut.length()];//[null, null, null, null, null]
Object[] array1=new Object[jsonArrayOut.length()];//[null, null, null, null, null]
for(int i=0;i<jsonArrayOut.length();i++){
//jsonArray根据索引,得到jsonObj
JSONObject ob = (JSONObject) jsonArrayOut.get(i);//{"colors":"Sony","value":7}
Object[] arr=new Object[ob.length()];//[null, null]
Object colors=ob.getString("colors");//Sony 备注:colors="Sony"
colors="'"+colors+"'";//'Sony' 备注:colors="'Sony'"
String value=ob.getString("value");//7 备注:value="7"
int valueInt=Integer.valueOf(value);//7 备注:valueInt=7
arr[0]=colors;//['Sony', null]
arr[1]=valueInt;//['Sony', 7]
array[i]=arr;
}
//数组array= [['Sony',7], ['Samsumg',14], ['LG',21], ['Vizio',34], ['Insignia', 24]];
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
js版 ——**.js
$(function(){
jsonObjArr.ComplexJSONStr_Array();//复杂json字符串转数组
})
var jsonObjArr={
/**
* @Description:复杂json字符串转数组
* JOSN格式:{"pieTes":[{"colors":"Sony","value":7},{"colors":"Samsumg","value":14},{"colors":"LG","value":21},{"colors":"Vizio","value":34},{"colors":"Insignia","value":24}]}
转换成 数组 [['Sony',7], ['Samsumg',14], ['LG',21], ['Vizio',34], ['Insignia', 24]];
*/
ComplexJSONStr_Array:function(){
var jsonObj = {"pieTes":[{"colors":"Sony","value":7},{"colors":"Samsumg","value":14},{"colors":"LG","value":21},{"colors":"Vizio","value":34},{"colors":"Insignia","value":24}]};//jsonObj = {pieTes: Array(5)}
var jsonArr =jsonObj.pieTes;//jsonArr = (5) [{…}, {…}, {…}, {…}, {…}]
var arr1 = new Array();//arr1=[]
/*for遍历*/
var len1=jsonArr.length;//len1 = 5, jsonArr = (5) [{…}, {…}, {…}, {…}, {…}]
//调用方法,获得jsonArr长度
var len2=jsonObjArr.JsonArrLength1(jsonArr);//len2 = 5, jsonArr = (5) [{…}, {…}, {…}, {…}, {…}]
var len3=JsonArrLength2(jsonArr);
for(var j=0;j<len2;j++){//j=0
var colors=jsonArr[j].colors;//colors = "Sony"
colors="'"+colors+"'";//colors = "'Sony'"
arr1.push([colors, jsonArr[j].value]);//arr = [Array(2)], jsonArr = (5) [{…}, {…}, {…}, {…}, {…}], j = 0
}
var arr2 = new Array();//arr2=[]
/*for in 遍历*/
for(var i in jsonArr) {//i = "0", jsonArr = (5) [{…}, {…}, {…}, {…}, {…}]
var colors=jsonArr[i].colors;//colors = "Sony"
//console.log(colors);//Sony
//alert(colors);//Sony
colors="'"+colors+"'";//colors = "'Sony'"
//console.log(colors);//'Sony'
//alert(colors);//'Sony'
arr2.push(colors, jsonArr[i].value);//arr = (2) ["'Sony'", 7], colors = "'Sony'", jsonArr = (5) [{…}, {…}, {…}, {…}, {…}], i = "0"
//console.log(arr);//(2) ["'Sony'", 7]
//alert(arr);//'Sony',7
}
},
/**
* 获取jsonArr的长度:此方法只能获取到第一层jsonArr的长度
*/
JsonArrLength1:function(jsonArr){
var len=0;
for(var i in jsonArr){
len++;
}
return len;
},
}
function JsonArrLength2(obj){
var len=0;
for(var i in obj){
len++;
}
return len;
}
热心网友
时间:2022-04-29 12:57
decode 一下