将 JSON 转换为数组 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14601494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:21:09  来源:igfitidea点击:

Convert JSON To Array Javascript

javascriptjqueryajaxjson

提问by user962206

I am currently receiving a JSON Object From the Server side of my application, the result is this

我目前正在从我的应用程序的服务器端接收一个 JSON 对象,结果是这样的

{"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

所以我想要的是那个 JSON 对象的数组表示

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

回答by Darin Dimitrov

Assuming you got your server side response in a javascript object called responseyou could parse the tagsstring property using the $.parseJSONfunction. 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):

假设您在一个名为的 javascript 对象中获得了服务器端响应,response您可以tags使用该$.parseJSON函数解析字符串属性。但首先您需要修复您的服务器端代码,以便它为 tags 属性返回一个有效的 JSON 字符串(在 JSON 属性名称中必须用引号括起来):

// 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 tagsproperty you could still use evalinstead of $.parseJSON:

如果由于某种原因您无法修改服务器端脚本以在tags属性中提供有效的 JSON,您仍然可以使用eval而不是$.parseJSON

var tags = eval(response.tags);

It's not a recommended approach, normally you should avoid using evalbecause it will execute arbitrary javascript.

这不是推荐的方法,通常你应该避免使用,eval因为它会执行任意的 javascript。

回答by Wilher Distu

initSelection: function (element, callback) {
                    var data = $(element).val();
                    callback($.parseJSON(data));
                }