将JSON字符串解析为数组,而不是对象

时间:2022-08-26 11:50:02

I've got a PHP array and echo that into javascript with json encode, i need to do it this way because it's going to be very dynamic. This is the code it echo's:

我有一个PHP数组并使用json编码将其回显到javascript中,我需要这样做,因为它会非常动态。这是它回应的代码:

{"notempty":true}

And i use this to, convert it to javascript:

我使用它,将其转换为javascript:

var myarray = eval('(' + json + ')');

For some reason it creates an object instead of an array and for that reason i cant use .length or a for loop.

由于某种原因,它创建一个对象而不是数组,因此我不能使用.length或for循环。

Does someone know what im doing wrong here?

有人知道我在这里做错了什么吗?

Thanks

4 个解决方案

#1


4  

You're trying to treat an Object like an Array, and an Object is not an Array, it is an Object.

您正在尝试将对象视为数组,而对象不是数组,它是一个对象。

Any time you see {} in JSON, that means "What is contained within these hallowed brackets is a dynamic object". When you see [], that means "Behold! I am an Array" (there are notable exceptions to this one: jQuery does some special work with to make itself look like an array).

每当你在JSON中看到{}时,这意味着“这些神圣的括号中包含的内容是动态对象”。当你看到[]时,这意味着“看哪!我是一个数组”(这个有一些值得注意的例外:jQuery做了一些特别的工作,使它自己看起来像一个数组)。

So, in order to iterate through an Object, you'll want to use for... in.

所以,为了遍历一个Object,你需要使用for ... in。

// eval BAD unless you know your input has been sanitized!.
var myObj = JSON.parse('{"notempty":true}');
// personally, I use it in for... in loops. It clarifies that this is a string
// you may want to use hasOwnProperty here as sometimes other "keys" are inserted
for( var it in myObj ) console.log( "myObj["+it+"] = " + myObj[it] );

#2


1  

{} is an object, which contains one attribute named notempty. If you want an array, it'd have to be

{}是一个对象,它包含一个名为notempty的属性。如果你想要一个数组,它必须是

[{"notempty":true}]

which is an array with a single element at index 0, which is an object with the single attribute 'notempty';.

这是一个在索引为0的单个元素的数组,它是一个具有单个属性'notempty';的对象。

#3


1  

By default, if you use encode an assoc array in php, it will become a js object when you decode. In order to have it be an array, you need to make it an array in php:

默认情况下,如果在php中使用编码关联数组,则在解码时它将成为js对象。为了让它成为一个数组,你需要在php中使它成为一个数组:

PHP:

$arr = "['notempty','notempty2','notempty3']";

Otherwise, you should convert it to an array in JS, but that seems to me a waste since looping through the object in javascript is so much easier:

否则,你应该将它转换为JS中的数组,但这似乎是浪费,因为在javascript中循环遍历对象要容易得多:

Javascript:

var arr = new Array();
for(var i in obj) arr[i] = obj[i];

#4


0  

You can use jQuery to parse it into an array like this:

您可以使用jQuery将其解析为如下数组:

var p = [];
$.each(jsonData, function (key, val) {
    p.push([val.propertyOne, val.propertyTwo]);
});

I am presuming of course that you want to parse JSON, not an array or any other string.

我当然假设您要解析JSON,而不是数组或任何其他字符串。

#1


4  

You're trying to treat an Object like an Array, and an Object is not an Array, it is an Object.

您正在尝试将对象视为数组,而对象不是数组,它是一个对象。

Any time you see {} in JSON, that means "What is contained within these hallowed brackets is a dynamic object". When you see [], that means "Behold! I am an Array" (there are notable exceptions to this one: jQuery does some special work with to make itself look like an array).

每当你在JSON中看到{}时,这意味着“这些神圣的括号中包含的内容是动态对象”。当你看到[]时,这意味着“看哪!我是一个数组”(这个有一些值得注意的例外:jQuery做了一些特别的工作,使它自己看起来像一个数组)。

So, in order to iterate through an Object, you'll want to use for... in.

所以,为了遍历一个Object,你需要使用for ... in。

// eval BAD unless you know your input has been sanitized!.
var myObj = JSON.parse('{"notempty":true}');
// personally, I use it in for... in loops. It clarifies that this is a string
// you may want to use hasOwnProperty here as sometimes other "keys" are inserted
for( var it in myObj ) console.log( "myObj["+it+"] = " + myObj[it] );

#2


1  

{} is an object, which contains one attribute named notempty. If you want an array, it'd have to be

{}是一个对象,它包含一个名为notempty的属性。如果你想要一个数组,它必须是

[{"notempty":true}]

which is an array with a single element at index 0, which is an object with the single attribute 'notempty';.

这是一个在索引为0的单个元素的数组,它是一个具有单个属性'notempty';的对象。

#3


1  

By default, if you use encode an assoc array in php, it will become a js object when you decode. In order to have it be an array, you need to make it an array in php:

默认情况下,如果在php中使用编码关联数组,则在解码时它将成为js对象。为了让它成为一个数组,你需要在php中使它成为一个数组:

PHP:

$arr = "['notempty','notempty2','notempty3']";

Otherwise, you should convert it to an array in JS, but that seems to me a waste since looping through the object in javascript is so much easier:

否则,你应该将它转换为JS中的数组,但这似乎是浪费,因为在javascript中循环遍历对象要容易得多:

Javascript:

var arr = new Array();
for(var i in obj) arr[i] = obj[i];

#4


0  

You can use jQuery to parse it into an array like this:

您可以使用jQuery将其解析为如下数组:

var p = [];
$.each(jsonData, function (key, val) {
    p.push([val.propertyOne, val.propertyTwo]);
});

I am presuming of course that you want to parse JSON, not an array or any other string.

我当然假设您要解析JSON,而不是数组或任何其他字符串。