1
这是来自服务器的JSON:ios解析包含数组的json?
{
"name":"channelname",
"args":
[
{
"username":"myusername",
"message":"mymessage"
}
]
}
使用的iOS5内置JSON方法我尝试解析出ARGS的用户名/消息。
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: [packet.data dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: nil];
NSDictionary *argsValues = [[NSDictionary alloc] initWithDictionary:[JSON objectForKey:@"args"]];
第二行抛出一个错误:
dictionary argument is not an NSDictionary
当我的NSLog [JSON objectForKey @ “ARGS”]我得到:
(
{
message = mymessage;
username = myusername;
}
)
我认为括号中打破它,不要不知道他们从哪里来,帮助表示赞赏。
编辑:
由于选择的答案,这里是我用来获取ARGS键的代码。
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData: [packet.data dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: nil];
NSArray *argsArray = [[NSArray alloc] initWithArray:[JSON objectForKey:@"args"]];
NSDictionary *argsDict = [[NSDictionary alloc] initWithDictionary:[argsArray objectAtIndex:0]];
NSLog(@"keys = %@", [argsDict allKeys]);