2011-05-31 99 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