将JSON字符串转换为多维数组

3
我似乎在将数组在PHP/JS之间进行转换时遇到了问题。我使用JavaScript中的XmlHttpRequest向一个PHP页面发送请求,该页面使用json_encode对多维(2D)数组进行编码。
当接收到字符串时,我使用JSON.parse()解码字符串,但它返回的是一个一维数组。有没有办法将JSON字符串解析成多维数组而不是单个维度?
以下是从CSV文件接收到的JSON示例:
[
    {
        "rating": "0",
        "title": "The Killing Kind",
        "author": "John Connolly",
        "type": "Book",
        "asin": "0340771224",
        "tags": "",
        "review": "i still haven't had time to read this one..."
    },
    {
        "rating": "0",
        "title": "The Third Secret",
        "author": "Steve Berry",
        "type": "Book",
        "asin": "0340899263",
        "tags": "",
        "review": "need to find time to read this book"
    },
    {
        "rating": "3",
        "title": "The Last Templar",
        "author": "Raymond Khoury",
        "type": "Book",
        "asin": "0752880705",
        "tags": "",
        "review": ""
    },
    {
        "rating": "5",
        "title": "The Traveller",
        "author": "John Twelve Hawks",
        "type": "Book",
        "asin": "059305430X",
        "tags": "",
        "review": ""
    },
    {
        "rating": "4",
        "title": "Crisis Four",
        "author": "Andy Mcnab",
        "type": "Book",
        "asin": "0345428080",
        "tags": "",
        "review": ""
    },
    {
        "rating": "5",
        "title": "Prey",
        "author": "Michael Crichton",
        "type": "Book",
        "asin": "0007154534",
        "tags": "",
        "review": ""
    },
    {
        "rating": "3",
        "title": "The Broker (Paperback)",
        "author": "John Grisham",
        "type": "Book",
        "asin": "0440241588",
        "tags": "book johngrisham",
        "review": "good book, but is slow in the middle"
    },
    {
        "rating": "3",
        "title": "Without Blood (Paperback)",
        "author": "Alessandro Baricco",
        "type": "Book",
        "asin": "1841955744",
        "tags": "",
        "review": ""
    },
    {
        "rating": "5",
        "title": "State of Fear (Paperback)",
        "author": "Michael Crichton",
        "type": "Book",
        "asin": "0061015733",
        "tags": "",
        "review": ""
    },
    {
        "rating": "4",
        "title": "The Rule of Four (Paperback)",
        "author": "Ian Caldwell",
        "type": "Book",
        "asin": "0099451956",
        "tags": "book bestseller",
        "review": ""
    },
    {
        "rating": "4",
        "title": "Deception Point (Paperback)",
        "author": "Dan Brown",
        "type": "Book",
        "asin": "0671027387",
        "tags": "book danbrown bestseller",
        "review": ""
    },
    {
        "rating": "5",
        "title": "Digital Fortress : A Thriller (Mass Market Paperback)",
        "author": "Dan Brown",
        "type": "Book",
        "asin": "0312995423",
        "tags": "book danbrown bestseller",
        "review": ""
    },
    {
        "rating": "5",
        "title": "Angels & Demons (Mass Market Paperback)",
        "author": "Dan Brown",
        "type": "Book",
        "asin": "0671027360",
        "tags": "book danbrown bestseller",
        "review": ""
    },
    {
        "rating": "4",
        "title": "The Da Vinci Code (Hardcover)",
        "author": "Dan Brown",
        "type": "   Book   ",
        "asin": "0385504209",
        "tags": "book movie danbrown bestseller davinci",
        "review": ""
    }
]

在解析后,如果我尝试使用myArr [0] [1]访问它,它会显示为未定义。

如果这很明显,请原谅我,我是JS和JSON的新手。


2
你能给一个你收到的JSON的例子吗? - bigblind
1
我已经更新了我的答案,现在你已经发布了JSON文本,展示了如何在反序列化后访问其中的数据。我的原始答案是针对一个数组中嵌套的数组,但你引用的实际上只是一个对象数组。 - T.J. Crowder
1个回答

5

编辑:根据您实际提供的JSON内容,您定义的JSON并不是定义一个二维数组,而是定义了一个包含对象的一维数组。在反序列化后,您可以通过以下方式访问第一本书的标题:

alert(myArray[0].title);

实时示例(请注意,在该示例中,我必须转义你提供的JSON中的单引号,以便将其全部放在JavaScript字符串中;不要让这个抛出你,你的JSON文本是正确和有效的,因为你引用了它,只是我将它放在由单引号分隔的字符串中,所以我必须转义其中的单引号)


原始答案

JavaScript数组始终是一维的,但每个数组中的条目本身可以是对数组的引用,从而有效地创建了二维数组。JSON在正确编码时不会有任何问题:

[
    [1, 2, 3],
    [4, 5, 6]
]

这是一个包含两个元素的数组的JSON表示法,每个元素都是一个包含三个元素的数组。

以下是使用Crockford的JSON字符串化和解析器的示例(以下语法现在已成为ECMAScript5的标准,但并非所有浏览器都支持):

var a, str, b;

a = [
    [1, 2, 3],
    [4, 5, 6]
];
display("a.length = " + a.length);
display("a[0] = " + a[0].join(","));
display("a[1] = " + a[1].join(","));
str = JSON.stringify(a);
display("JSON string: " + str);
b = JSON.parse(str);
display("b.length = " + b.length);
display("b[0] = " + b[0].join(","));
display("b[1] = " + b[1].join(","));

输出:

a.length = 2
a[0] = 1,2,3
a[1] = 4,5,6
JSON字符串: [[1,2,3],[4,5,6]]
b.length = 2
b[0] = 1,2,3
b[1] = 4,5,6

查看实例


感谢您提供详细的帮助。不过,在不再提出问题的情况下,是否有一种简单的方法可以将对象数组中的数据转储到数组数组中,还是必须逐个循环并逐个提取? - Speedy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接