問題:
I have a JSON string (from PHP's json_encode()
that looks like this:我有一個 JSON 字符串(來自 PHP 的json_encode()
,如下所示:
[{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}]
I want to parse this into some sort of data structure for my iPhone app.我想將它解析爲我的 iPhone 應用程序的某種數據結構。 I guess the best thing for me would be to have an array of dictionaries, so the 0th element in the array is a dictionary with keys "id" => "1"
and "name" => "Aaa"
.我想對我來說最好的事情是擁有一個字典數組,所以數組中的第 0 個元素是一個字典,鍵爲"id" => "1"
和"name" => "Aaa"
。
I do not understand how the NSJSONSerialization
stores the data though.我不明白NSJSONSerialization
如何存儲數據。 Here is my code so far:到目前爲止,這是我的代碼:
NSError *e = nil;
NSDictionary *JSON = [NSJSONSerialization
JSONObjectWithData: data
options: NSJSONReadingMutableContainers
error: &e];
This is just something I saw as an example on another website.這只是我在另一個網站上看到的一個例子。 I have been trying to get a read on the JSON
object by printing out the number of elements and things like that, but I am always getting EXC_BAD_ACCESS
.我一直試圖通過打印元素的數量和類似的東西來讀取JSON
對象,但我總是得到EXC_BAD_ACCESS
。
How do I use NSJSONSerialization
to parse the JSON above, and turn it into the data structure I mentioned?如何使用NSJSONSerialization
解析上面的 JSON,並將其轉化爲我提到的數據結構?
解決方案:
參考一: https://en.stackoom.com/question/Z3zm參考二: https://stackoom.com/question/Z3zm