2011-05-31 98 views
3

我似乎有一个问题转换数组之间来回转发PHP/JS之间。我正在使用JavaScript中的XmlHttpRequest到使用json_encode编码多维(2D)数组的PHP页面。JSON字符串到多维阵列

当接收到字符串时,我使用JSON.parse()来解码字符串,但它作为一维数组返回。有没有办法将JSON字符串解析为多维数组而不是单维?收到的JSON(从CSV文件)的

实施例:

[ 
    { 
     "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 2011-05-31 16:41:38

+1

我已经更新了我的答案,现在您已经发布了JSON文本,以显示如何在访问其中的数据后对其进行反序列化。我原来的答案是针对数组的数组,但是你引用的实际上只是一个对象数组。 – 2011-05-31 16:51:56

回答

5

编辑:你的编辑显示您的实际JSON后:

您已经定义了JSON没有定义二维数组,它定义了一个一维的对象数组。一旦反序列化,你会访问(说)的第一本书,像这样的标题:

alert(myArray[0].title); 

Live example(注意,在例子中,我不得不逃避你为了给了JSON单引号把整个东西放在一个JavaScript字符串中;不要让它抛出你,你的JSON文本是正确和有效的,正如你引用它,只是我把它放在一个用单引号分隔的字符串中,所以我必须逃脱其中的单引号)


原来的答复

JavaScript数组总是一维的,但数组中的每个本身可以是对数组的引用,这有效地用于二维数组。当正确编码JSON与这些没有问题:

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

这是JSON对象具有两个条目,每个条目的是三个条目的数组的数组。

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 string: [[1,2,3],[4,5,6]] 
b.length = 2 
b[0] = 1,2,3 
b[1] = 4,5,6

Live copy

使用Crockford的JSON stringifier和解析器(以下语法现在也作为ECMAScript5的标准,但不是所有的浏览器都有它尚未)

+0

感谢您的详细帮助。在不问另一个问题的情况下,是否有一种简单的方法可以将数组中的数据转储为数组数组,还是必须循环遍历每个对象并逐个提取它? – Speedy 2011-05-31 17:04:44