我有以下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."}]
答案 0 :(得分:0)
您的文字看起来像一个数组。所以试试这样: -
NSError *err = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &err];
答案 1 :(得分:0)
如果您的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];
现在您可以像字典一样访问元素。 希望这会对你有所帮助。