转换JSON为阵的Javascript [英] Convert JSON To Array Javascript
问题描述
我目前收到的JSON对象从我的应用程序的服务器端,结果是这样的
{标签:[{值:2,标签:'的Dubstep},{值:3,标签:'BoysIIMen},{值:4,标签: 'Sylenth1'}]}
但我并不真正需要的标签,结果中的双引号。
所以,我要的是重新的JSON对象的presentation
数组因此,我将如何转换这种
{标签:[{值:2,标签:'的Dubstep},{值:3,标签:'BoysIIMen},{值:4,标签: 'Sylenth1'}]}
本
[{值:2,标签:'的Dubstep},{值:3,标签:'BoysIIMen},{值:4,标签:'Sylenth1'}]
下面是创建数组循环
字符串K =[;
名单<标签> TG = audioTaggingService.findTagsByName(q)的;
的for(int i = 0; I< audioTaggingService.findTagsByName(Q).size();我++){
标签吨= tg.get(ⅰ);
如果(ⅰ==(tg.size() - 1)){
K + ={值:+ t.getId()+的标签:+ t.getName()+'};
}其他{
K + ={值:+ t.getId()+的标签:+ t.getName()+'};
}
}
K + =];
的code以上的结果是这样的
[{值:2,标签:'的Dubstep},{值:3,标签:'BoysIIMen},{值:4,标签:'Sylenth1'}]
假设你有在被称为JavaScript对象服务器端的响应响应
你能解析<$ C使用 $。parseJSON
函数$ C>标签字符串属性。但首先,你需要修复你的服务器端code,这样它会返回一个有效的JSON字符串变量属性(在JSON属性名必须用引号括起来):
//这是来自服务器
VAR响应= {标签:[{\价值\:2,\标签\:\的Dubstep \},{\价值\:3,\标签\:\BoysIIMen \},{\价值\:4,\标签\:\Sylenth1 \}]};
//现在你可以解析的标签字符串属性转换成相应的
// JavaScript数组:
VAR标签= $ .parseJSON(response.tags);
//并在此阶段的标签对象将包含所需阵列
//你就可以访问它的单个元素:
警报(标签[0] .label);
如果由于某种原因,你不能修改服务器端脚本来提供一个有效的JSON在标签
属性,你仍然可以使用评估$ 。C $℃,而不是
$> parseJSON
:
VAR标签=的eval(response.tags);
这不是一个推荐的方法,一般应避免使用评估
,因为它会执行任意JavaScript。
I am currently receiving a JSON Object From the Server side of my application, the result is this
{"tags":"[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]"}
But then I don't really need the "tags" and the double quotes in the result.
So what I want is an array representation of that JSON object
therefore how would I convert this
{"tags":"[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]"}
to this
[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]
Here's the loop that creates the array
String k = "[";
List<Tag> tg = audioTaggingService.findTagsByName(q);
for(int i = 0; i<audioTaggingService.findTagsByName(q).size();i++){
Tag t = tg.get(i);
if(i == (tg.size() - 1)){
k+="{value: "+t.getId()+",label:'"+t.getName()+"'}";
}else{
k+="{value: "+t.getId()+",label:'"+t.getName()+"'}";
}
}
k+="]";
The result of the code above is this
[{value: 2,label: 'Dubstep'},{value: 3,label: 'BoysIIMen'},{value: 4,label:'Sylenth1'}]
Assuming you got your server side response in a javascript object called response
you could parse the tags
string property using the $.parseJSON
function. But first you will need to fix your server side code so that it returns a valid JSON string for the tags property (in JSON property names must be enclosed in quotes):
// This came from the server
var response = {"tags":"[{\"value\": 2,\"label\": \"Dubstep\"},{\"value\": 3,\"label\": \"BoysIIMen\"},{\"value\": 4,\"label\":\"Sylenth1\"}]"};
// Now you could parse the tags string property into a corresponding
// javascript array:
var tags = $.parseJSON(response.tags);
// and at this stage the tags object will contain the desired array
// and you could access individual elements from it:
alert(tags[0].label);
If for some reason you cannot modify your server side script to provide a valid JSON in the tags
property you could still use eval
instead of $.parseJSON
:
var tags = eval(response.tags);
It's not a recommended approach, normally you should avoid using eval
because it will execute arbitrary javascript.
这篇关于转换JSON为阵的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!