【问题标题】:json and multidimensional arrayjson和多维数组
【发布时间】:2012-03-07 13:23:04
【问题描述】:

我有一个这样的多维数组

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

我通过

将此编码为json
json_encode($array);

生成的 jsonString 是这样的

{"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"}}

当我做警报时(jsonString.length);结果是 4 但我希望结果是 2 我做错了什么。

【问题讨论】:

  • the result is 4你确定它与你的json有关吗?对象不支持长度属性。比较alert({}.length);alert([].length);
  • 我认为我的 json 中缺少一个 [ 但我不知道如何添加它..

标签: javascript arrays json multidimensional-array


【解决方案1】:

一个对象字面量没有.length

您可以使用this method 计算属性:

var count = 0;

for (i in jsonString) {
    if (jsonString.hasOwnProperty(i)) {
        count++;
    }
}

alert(count); //count shall have length for you

由于您的数组没有数字索引(从 0 开始),它假定您使用了一个 关联数组,因此他们转储了一个 项目对象,而不是项目数组

要将它们转换为数字索引,您所要做的就是在将它们编码为 json 之前使用array_values

json_encode(array_values($array));

那么 json 将是一个数组..then you can use length

从此:

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       
    )
);

使用 array_values() 变成这样,注意每个项目的索引:

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       
    )
);

然后编码为json并存储到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 数组有问题我缺少 [ 括号吗?
  • 阅读我的更新,它包含在第二种方法中。
【解决方案2】:

对象不支持长度属性:alert({}.length); 提供 undefinedalert([].length); 提供 0。要找到顶层的“长度”,您可以这样做:

var arr ={"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"}};
var len = 0;
for(var i in arr) len++;
alert(len);

http://jsfiddle.net/uszaV/

【讨论】:

  • 我的 json 数组有问题我缺少 [ 括号吗?
  • @ubercooluk 没有错,当您将关联数组保存为 json 格式时,它会变成一个对象以保留键。比较echo json_encode(array(1,2));echo json_encode(array('a'=>1,'b'=>2));
猜你喜欢
  • 2011-08-08
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 2013-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
相关资源
最近更新 更多