我似乎有一個問題轉換數組之間來回轉發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
你能舉一個你收到的JSON的例子嗎? – bigblind 2011-05-31 16:41:38
我已經更新了我的答案,現在您已經發布了JSON文本,以顯示如何在訪問其中的數據後對其進行反序列化。我原來的答案是針對數組的數組,但是你引用的實際上只是一個對象數組。 – 2011-05-31 16:51:56