I have observed that in php you can encode an array and the resulting json does not carry the square brackets.However,in my javascript array,
var arrCars = new Array("Toyota", "Mercedes", "BMW");
var jsonStr = JSON.stringify(arrCars);
alert(jsonStr);
i keep getting the square brackets.I have also noticed that if i use json stringfy,
var foo = {};
foo.bar = "new property";
foo.baz = 3;
var JSONfoo = JSON.stringify(foo);
i get the json without the square just like i wanted.What must i do to my array to do away with the brackets?.
I have observed that in php you can encode an array and the resulting json does not carry the square brackets.However,in my javascript array,
var arrCars = new Array("Toyota", "Mercedes", "BMW");
var jsonStr = JSON.stringify(arrCars);
alert(jsonStr);
i keep getting the square brackets.I have also noticed that if i use json stringfy,
var foo = {};
foo.bar = "new property";
foo.baz = 3;
var JSONfoo = JSON.stringify(foo);
i get the json without the square just like i wanted.What must i do to my array to do away with the brackets?.
Share Improve this question asked Jun 24, 2012 at 19:41 GandalfGandalf 13.7k34 gold badges104 silver badges178 bronze badges 7-
What do you mean?
echo json_encode(array("item1", "item2", "item3"));
gives me the expected["item1","item2","item3"]
. Also, why would you want to get rid of the brackets? They are a crucial part of JSON. – Madara's Ghost Commented Jun 24, 2012 at 19:44 - The example i looked at did not produce the brackets.Its php/manual/en/function.json-encode.php – Gandalf Commented Jun 24, 2012 at 19:50
- 1 That won't produce a javascript array, it will produce an object. If you use an associative array in PHP, you'll get that result. – Madara's Ghost Commented Jun 24, 2012 at 19:51
- Can any array in javascript produce that result?. – Gandalf Commented Jun 24, 2012 at 19:52
-
1
If the brackets are a problem,get rid of them yourself.
JSON.stringify(arrCars).replace(/[\[\]]/g,'')
you should probably be more careful/selective about how you do that, but just as a simple example – nbrooks Commented Jun 24, 2012 at 19:57
3 Answers
Reset to default 2There's a difference between an array ([]
) and an object ({}
) in javascript. Your first example uses an array. Your second example uses an object. The main difference is that when you have an array ([]
) the index can only be a zero based integer. Whereas an object can have property names that are strings.
The correct JSON for the array in your first example is ["Toyota","Mercedes","BMW"]
, and the correct JSON for the object in your second example is either {"bar":"new property","baz":3}
or {"baz":3,"bar":"new property"}
.
A JSON string contains either an object or an array, so it always has either curly brackets {}
or square brackets []
.
If you get anything else, the result is not correct. If you expect anything else, your expectation is wrong.
If you want the curly brackets instead of the square brackets, you have to serialise an object, not an array.
Not sure why you don't want brackets, since brackets are the normal way to make an array literal (as opposed to using the Array() function, as you do, which is way old-school)
If you want the JSON without the brackets, such as if you are building a bigger JSON string or something, you can use something like this:
function jsonifyArrayWithoutBrackets(a) {
var output = [];
for (var i=0; i<a.length; i++)
output.push(JSON.stringify(a[i]));
return output.join(',');
}
Or, obviously, just trim the brackets off after using a single call to JSON.stringify().
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745232268a4617731.html
评论列表(0条)