怎么把 json 格式转换成 数组格式?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么把 json 格式转换成 数组格式?相关的知识,希望对你有一定的参考价值。
怎么把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]];
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);
参考技术Aphp直接json_decode($json,true) 这样就可以得到数组的
js的话 var array = eval(json),然后将array循环,就可以得到一个数组
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;
参考技术C decode 一下
js怎么把数组转换成json字符串
1、javascript里面的数组都是关联数组吧?js对象的本质就是关联数组。索引数组也就是常说的数组,数组是对象,所以数组的本质也是关联数组,但通常没人这么说。2、唯一区别就是:索引数组的索引只能是0和正整数,但它是有序的。关联数组的索引很宽松,但关联数组是无序的,就这个。3、json对象是严格版本的关联数组。4、至少在chrome下,索引数组和关联数组的速度是一样的。5、索引数组的好处就是格式简单,关联数组的好处是键可以任意定义。现代浏览器中提供了JSON.stringify()方法将数组,对象转成json。JSON.stringify把一个对象转换成json字符串,JSON.parse把一个json字符串解析成对象。不支持的可以引入json2.js$.fn.stringifyArray=function(array)returnJSON.stringify(array)$.fn.parseArray=function(array)returnJSON.parse(array)然后调用:$("").stringifyArray(array) 参考技术AJS 字面量对象 转成 json 字符串
var arr = [1,2,3, a : 1 ];JSON.stringify( arr );
字符串解析成 JSON
var str = '[1,2,3,"a":1]';JSON.parse( str );本回答被提问者采纳 参考技术B var jsonstr=JSON.stringify(你的数组);
以上是关于怎么把 json 格式转换成 数组格式?的主要内容,如果未能解决你的问题,请参考以下文章