我在解析 json 对象的教程中找到了这部分代码:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);
// convert to JSON
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
// show all values
for(id key in res) {
id value = [res objectForKey:key];
NSString *keyAsString = (NSString *)key;
NSString *valueAsString = (NSString *)value;
NSLog(@"key: %@", keyAsString);
NSLog(@"value: %@", valueAsString);
}
// extract specific value...
NSArray *results = [res objectForKey:@"results"];
for (NSDictionary *result in results) {
NSString *icon = [result objectForKey:@"icon"];
NSLog(@"icon: %@", icon);
}
}
当我的 JSON 对象采用这种形式时,我能够使用该代码并解析数据:
{"data_1":0,"data_2":0,"Plato 1":0,"data_3":0....}
我只是在搜索特定的键,名为 data_1 等等。
现在我通过 DB 获取数据,所以我的 JSON 对象是一个数组。
JSON 对象是这种形式:
[{"0":"1","id":"1","1":"text1","image":"text2","2":"0"}]
我必须做出哪些改变?