2015-01-05 68 views
-4

我有以下JSON文本如下。我想將它整合到我的應用程序中,並且能夠將它的一部分分配給要使用的變量。簡單,我知道,但我無法弄清楚如何去做。如何在我的應用程序中使用此JSON文本?

[{"id":"1","category":"1","fact":"Worldwide, around 1.2 trillion eggs are produced for eating every year, which means that average person on Earth consumes 173 eggs a year."},{"id":"3","category":"2","fact":"How to tell if you're going to throw up? -Your mouth starts to fill with saliva which is there to protect your mouth and teeth from the stomach acid mixed with the vomit."},{"id":"2","category":"2","fact":"Bone cells are constantly renew so every ten years you have a new skeleton."}] 
+2

在'NSJSONSerialization'上做一些搜索。 – rmaddy

+0

請注意,如果您在Google上搜索「objective-c json」,您將獲得數十個示例。 –

回答

0

您的文本看起來是一個數組。因此,嘗試這樣的: -

NSError *err = nil; 
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &err]; 
0

如果你的JSON文本是你需要將其轉換爲NSData的這麼的NSString:

NSString* jsonText = @"[{"id":"1","category":"1","fact":"Worldwide, around 1.2 trillion eggs are produced for eating every year, which means that average person on Earth consumes 173 eggs a year."},{"id":"3","category":"2","fact":"How to tell if you're going to throw up? -Your mouth starts to fill with saliva which is there to protect your mouth and teeth from the stomach acid mixed with the vomit."},{"id":"2","category":"2","fact":"Bone cells are constantly renew so every ten years you have a new skeleton."}]"; 
    NSData* data = [jsonText dataUsingEncoding:NSUTF8StringEncoding]; 

    NSError* error; 
    NSDictionary* jsonDict = [NSJSONSerialization 
    JSONObjectWithData:responseData 
    options:kNilOptions 
    error:&error]; 

所以現在你可以訪問像一本字典的元素。 希望這可以幫助你。

相關問題