Objective c 如何在我的应用程序中使用此JSON文本?

Objective c 如何在我的应用程序中使用此JSON文本?,objective-c,json,Objective C,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

我有以下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."}] 

您的文本看起来像一个数组。所以试着这样做:-

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

如果json文本是NSString,则需要将其转换为NSData,以便:

    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];
现在,您可以像访问字典一样访问元素。
希望这对您有所帮助。

对NSJSONSerialization进行一些搜索。请注意,如果您在Google中搜索objective-c json,您将得到几十个示例。