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.
Example:
var s=[
'{"Select":"11", "PhotoCount":"12"}',
'{"Select":"21", "PhotoCount":"22"}',
'{"Select":"31", "PhotoCount":"32"}'];
-
10That's already a JavaScript array of objects!bfavaretto– bfavaretto2012年04月27日 19:00:20 +00:00Commented Apr 27, 2012 at 19:00
-
3Please confirm that you actually have a JavaScript array of JavaScript objects (as you typed in your question). If so, please explain what you want (since the title of your question is entirely misleading if this is the case).Phrogz– Phrogz2012年04月27日 19:07:06 +00:00Commented Apr 27, 2012 at 19:07
-
Yes it is a javascript array of json string objectsAlaa Osta– Alaa Osta2012年04月27日 19:28:02 +00:00Commented Apr 27, 2012 at 19:28
-
I've edited your question to match what you just said. Please ensure that you have used the correct terminology, that what you see in the question now matches what you have.Phrogz– Phrogz2012年04月27日 19:38:50 +00:00Commented Apr 27, 2012 at 19:38
3 Answers 3
If you have a JS array of JSON objects:
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 tests for browser comparisons.
If you have a single JSON string representing an array of objects:
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 string representing an array of objects
var json = JSON.stringify(s);
…or if you want a JavaScript array of JSON strings, then:
// 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]);
8 Comments
var objs = []; for (var i=s.length;i--;) objs[i] = JSON.parse(s[i]);; see my editsvar 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.
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
Edit (30 April 2020):
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>")
4 Comments
jQuery.parseJSON() method.If you really have:
var s = ['{"Select":"11", "PhotoCount":"12"}','{"Select":"21", "PhotoCount":"22"}'];
then simply:
var objs = $.map(s, $.parseJSON);
4 Comments
s, . Try again.