我想在iOS上用SBJSon库解析那些具有这种结构的Json 谁能帮我?非常感谢!
{“error”:{“用户名”:[“用户名已被删除。”],“电子邮件”:[“电子邮件已被删除。”]}} < / p>
答案 0 :(得分:2)
NSString *str=@"{\"error\":{\"username\":[\"The username has already been taken.\"],\"email\":[\"The email has already been taken.\"]}}";
NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: nil];
NSLog(@"dic is %@",json);
//输出
dic is {
error = {
email = (
"The email has already been taken."
);
username = (
"The username has already been taken."
);
};
}
使用SBJSon
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [str JSONValue];
答案 1 :(得分:0)
SBJsonParser * parser = [[SBJsonParser alloc] init];
NSObject * responseobj = [parser objectWithData:data]; // for NSData
NSObject * responseobj = [parser objectWithString:string]; // for NSString
在您的情况下,“responseobj”的类型为NSDictionary
。