Javascript 将 JSON 对象字符串数组转换为 JS 对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10356370/
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 01:00:50  来源:igfitidea点击:

Convert array of JSON object strings to array of JS objects

javascriptjqueryjson

提问by Alaa Osta

I would like to convert an array of JSON String to array of JSON object without looping through each item and parse it using JSON.parse.

我想将 JSON 字符串数组转换为 JSON 对象数组,而不需要遍历每个项目并使用 JSON.parse 对其进行解析。

Example:

例子:

var s=[
  '{"Select":"11", "PhotoCount":"12"}',
  '{"Select":"21", "PhotoCount":"22"}',
  '{"Select":"31", "PhotoCount":"32"}'];

回答by Phrogz

If you have a JS array of JSON objects:

如果您有一个 JS JSON 对象数组:

var s=['{"Select":"11","PhotoCount":"12"}','{"Select":"21","PhotoCount":"22"}'];

and you want an array of objects:

并且你想要一个对象数组:

// JavaScript array of JavaScript objects
var objs = s.map(JSON.parse);

// ...or for older browsers
var objs=[];
for (var i=s.length;i--;) objs[i]=JSON.parse(s[i]);

// ...or for maximum speed:
var objs = JSON.parse('['+s.join(',')+']');

See the speed testsfor browser comparisons.

请参阅浏览器比较的速度测试



If you have a single JSON string representing an array of objects:

如果您有一个表示对象数组的 JSON 字符串:

var s='[{"Select":"11","PhotoCount":"12"},{"Select":"21","PhotoCount":"22"}]';

and you want an array of objects:

并且你想要一个对象数组:

// JavaScript array of JavaScript objects
var objs = JSON.parse(s);


If you have an array of objects:

如果您有一组对象:

// A JavaScript array of JavaScript objects
var s = [{"Select":"11", "PhotoCount":"12"},{"Select":"21", "PhotoCount":"22"}];

…and you want JSON representation for it, then:

......并且你想要它的 JSON 表示,然后:

// JSON string representing an array of objects
var json = JSON.stringify(s);

…or if you want a JavaScript array of JSON strings, then:

……或者如果你想要一个 JSON 字符串的 JavaScript 数组,那么:

// JavaScript array of strings (that are each a JSON object)
var jsons = s.map(JSON.stringify);

// ...or for older browsers
var jsons=[];
for (var i=s.length;i--;) jsons[i]=JSON.stringify(s[i]);

回答by KBN

var json = jQuery.parseJSON(s); //If you have jQuery.

Since the comment looks cluttered, please use the parse function after enclosing those square brackets inside the quotes.

由于注释看起来很杂乱,请在将这些方括号括在引号内后使用 parse 函数。

var s=['{"Select":"11","PhotoCount":"12"}','{"Select":"21","PhotoCount":"22"}'];

Change the above code to

把上面的代码改成

var s='[{"Select":"11","PhotoCount":"12"},{"Select":"21","PhotoCount":"22"}]';

Eg:

例如:

$(document).ready(function() {
    var s= '[{"Select":"11","PhotoCount":"12"},{"Select":"21","PhotoCount":"22"}]';

    s = jQuery.parseJSON(s);

    alert( s[0]["Select"] );
});

And then use the parse function. It'll surely work.

然后使用解析函数。它肯定会起作用。

EDIT :Extremely sorry that I gave the wrong function name. it's jQuery.parseJSON

编辑:非常抱歉,我给出了错误的函数名称。它是 jQuery.parseJSON

Jquery

查询

The json api

json api

Edit (30 April 2020):

编辑(2020 年 4 月 30 日):

Editing since I got an upvote for this answer. There's a browser native function available instead of JQuery (for nonJQuery users), JSON.parse("<json string here>")

编辑,因为我得到了这个答案的赞成票。有一个浏览器本机功能可以代替 JQuery(对于非 JQuery 用户),JSON.parse("<json string here>")

回答by Ry-

If you really have:

如果你真的有:

var s = ['{"Select":"11", "PhotoCount":"12"}','{"Select":"21", "PhotoCount":"22"}'];

then simply:

然后简单地:

var objs = $.map(s, $.parseJSON);

Here's a demo.

这是一个演示。