JSON和多维数组 [英] json and multidimensional array
问题描述
我有一个多维数组这样
阵列
(
[1] =>排列
(
[PRODUCT_ID] => 1
[product_model] => HFJ5G1.5
[产品类型] =>高原
[product_return] => graviteits ) [2] =>排列
(
[PRODUCT_ID] => 2
[product_model] => HHJ5S2.5
[产品类型] =>赫勒plunjer
[product_return] =>德维尔 )
); //只有2显示在这里我身边有110值
和我带codeD这JSON通过
json_en code($数组);
所得jsonString是这样的
<$p$p><$c$c>{\"1\":{\"product_id\":\"1\",\"product_model\":\"HFJ5G1.5\",\"product_type\":\"plat\",\"product_return\":\"graviteits\"},\"2\":{\"product_id\":\"2\",\"product_model\":\"HHJ5S2.5\",\"product_type\":\"holle plunjer,product_return:转向}}当我这样做警报(jsonString.length);结果是4但我希望得到的结果是凌晨2点我做错了什么。
对象文本没有。长度
您可以使用这种方法计数属性:
VAR计数= 0;对(在jsonString我){
如果(jsonString.hasOwnProperty(ⅰ)){
算上++;
}
}警报(计数); //计数应具有的长度为你
或
因为你的数组没有数字索引(从0开始),它假设你使用了的关联数组的,因此他们的抛投,而项目的对象的比的项目阵列的
他们转向数字索引,所有你需要做的就是利用 array_values 编码他们到JSON之前:
json_en code(array_values($阵列));
那么JSON将是一个数组.. 那么你可以使用长度
从这个:
阵列(
[1] =&GT;阵列(
[PRODUCT_ID] =&GT; 1
[product_model] =&GT; HFJ5G1.5
[产品类型] =&GT;高原
[product_return] =&GT; graviteits
)
[2] =&GT;阵列(
[PRODUCT_ID] =&GT; 2
[product_model] =&GT; HHJ5S2.5
[产品类型] =&GT;赫勒plunjer
[product_return] =&GT;德维尔
)
);
就变成这个使用array_values(),请注意每件索引:
阵列(
[0] =&GT;阵列(
[PRODUCT_ID] =&GT; 1
[product_model] =&GT; HFJ5G1.5
[产品类型] =&GT;高原
[product_return] =&GT; graviteits
)
[1] =&GT;阵列(
[PRODUCT_ID] =&GT; 2
[product_model] =&GT; HHJ5S2.5
[产品类型] =&GT;赫勒plunjer
[product_return] =&GT;德维尔
)
);
然后连接coded到JSON和存储到jsonString:
jsonString = [
{
PRODUCT_ID:1,
product_model:HFJ5G1.5,
产品类型:高原,
product_return:graviteits
},
{
PRODUCT_ID:2,
product_model:HHJ5S2.5,
产品类型:赫勒plunjer
product_return:转向
}
];警报(jsonString.length);
I have a multidimensional array like this
Array
(
[1] => Array
(
[product_id] => 1
[product_model] => HFJ5G1.5
[product_type] => plat
[product_return] => graviteits
)
[2] => Array
(
[product_id] => 2
[product_model] => HHJ5S2.5
[product_type] => holle plunjer
[product_return] => veer
)
); //Only 2 are shown here i have around 110 values
And i encoded this to json by
json_encode($array);
The resulted jsonString is something like this
{"1":{"product_id":"1","product_model":"HFJ5G1.5","product_type":"plat","product_return":"graviteits"},"2":{"product_id":"2","product_model":"HHJ5S2.5","product_type":"holle plunjer","product_return":"veer"}}
when i do alert(jsonString.length); the result is 4 But i want the result to be 2 am i doing something wrong .
an object literal has no .length
you can count properties using this method:
var count = 0;
for (i in jsonString) {
if (jsonString.hasOwnProperty(i)) {
count++;
}
}
alert(count); //count shall have length for you
OR
since your array didn't have numeric indices (starting from 0), it assumed you used an associative array, hence they dumped an object of items rather than an array of items.
to turn them to numeric indices, all you have to do is use array_values before encoding them to json:
json_encode(array_values($array));
then the json will be an array.. then you can use length
from this:
Array(
[1] => Array(
[product_id] => 1
[product_model] => HFJ5G1.5
[product_type] => plat
[product_return] => graviteits
)
[2] => Array(
[product_id] => 2
[product_model] => HHJ5S2.5
[product_type] => holle plunjer
[product_return] => veer
)
);
it becomes this using array_values(), note the indexes per item:
Array(
[0] => Array(
[product_id] => 1
[product_model] => HFJ5G1.5
[product_type] => plat
[product_return] => graviteits
)
[1] => Array(
[product_id] => 2
[product_model] => HHJ5S2.5
[product_type] => holle plunjer
[product_return] => veer
)
);
then encoded to json and stored to jsonString:
jsonString = [
{
"product_id": "1",
"product_model": "HFJ5G1.5",
"product_type": "plat",
"product_return": "graviteits"
},
{
"product_id": "2",
"product_model": "HHJ5S2.5",
"product_type": "holle plunjer",
"product_return": "veer"
}
];
alert(jsonString.length);
这篇关于JSON和多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!