iphone - 使用 iOS 5 在 iPhone 中使用 JSON 数据

标签 iphone objective-c ios

如何在 iOS 5 中使用此 JSON 数据

({assets = ( { identity = 34DL3611;}, {identity = 34GF0512;}, {identity = 34HH1734;}, {identity = 34HH1736;}, {identity = 34YCJ15;} );

identity = DEMO;})

通过此调用在控制台上获取此数据

    id list =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

NSLog(@"VLIST: %@", list);

现在我在使用encoding:NSUTF8StringEncoding后得到了精确的JSON格式的数据,我想使用iOS 5的原生jsonserializer

NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

JSON 数据是:

[{"assets":[{"identity":"34DL3611"},{"identity":"34GF0512"},{"identity":"34HH1734"},{"identity":"34HH1736"},{"identity":"34YCJ15"}],"identity":"DEMO"}]

现在我如何获取这些数据,以便获取 Assets 数组值并将它们填充到表中并获取标识值(即 DEMO)以将其用作 header 。

谢谢

最佳答案

您似乎能够成功解析 JSON 数据,现在想知道如何访问数据。解析的 JSON 数据是 NSDictionaryNSArray 实例,包含 NSDictionaryNSArrayNSString, NSNumber 等实例。

从您的样本数据来看,您的数据似乎嵌套得很深。 (目的不是很清楚。)它是一个包含字典的数组,字典包含一个包含字典的数组。

你可以像这样访问它:

NSArray list =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSDictionary dict = [list objectAtIndex: 0];
NSArray assets = [dict objectForKey: @"assets"];
NSString identity = [dict objectForKey: @"identity"];
for (NSUInteger index = 0; index < [assets count]; index++) {
    NSDictionary itemDict = [assets objectAtIndex: index];
    NSString itemIdentity = [itemDict objectForKey: @"identity"];
}

关于iphone - 使用 iOS 5 在 iPhone 中使用 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8743372/

相关文章:

iphone - Cocoa Touch 中的音调生成

iphone - UITextField 在 UITableView 中滚动时清除 - 自定义单元格

iphone - UIView 3DCube 像 Clear-app

iphone - 使用用户触摸输入更改 View 大小

html - 是否可以让 UIWebView 加载 html 字符串,但表现得像 UILabel

ios - RKMappingresult 在带有 restkit 的 64 位 iPhone 中返回不同的结果

objective-c - NSSet 使用谓词返回匹配给定类的对象

objective-c - 使用 UIImage 定义背景图像

iphone 应用程序 webview 链接操作

iOS REST 调用和缓存策略