我的JSON字符串是:
[
{'Local':'webaddress'},
{'QA':'webaddress1'}
]
我的代码是:
NSMutableDictionary *dataDictonary = [NSJSONSerialization
JSONObjectWithData:responseData
options:0
error:nil];
NSArray *keys = [dataDictonary allKeys];
NSArray *values = [dataDictonary allValues];
int i=0;
NSLog(@"",[keys count]);
NSLog(@"",[values count]);
int i=0;
for ( NSString *items in keys )
{
NSLog(@"----");
NSLog(@"Name: %@", items);
NSLog(@"Address: %@", values[i++]);
NSLog(@"----");
}
这里我得到的大小在NSlog中没什么空白而且不能解析这个值不要不为什么。请帮忙..
答案 0 :(得分:3)
您的JSON是一个包含对象的数组,因此您需要将其转换为NSArray:
NSString *json_string = @"[{\"Local\": \"webaddress\" }, {\"QA\": \"webaddress1\" }]";
NSError *error;
NSArray *JSON =
[NSJSONSerialization JSONObjectWithData: [json_string dataUsingEncoding:NSUTF8StringEncoding]
options: NSJSONReadingMutableContainers
error: &error];
NSLog(@"Local: %@", JSON[0][@"Local"]); // output is: Local: webaddress
<强>更新强>
// itherate through array
for(NSDictionary *dictionary in JSON)
{
//now you can iterate throug each dicitonary
NSEnumerator *enumerator = [dictionary keyEnumerator];
id key;
while((key = [enumerator nextObject])){
NSLog(@"key=%@ value=%@", key, [dictionary objectForKey:key]);
}
}
日志如下所示:
key =本地值= webaddress
key = QA值= webaddress1