我想用 SBJSon libs 解析那些在 iOS 上具有类似结构的 Json 有人可以帮助我吗?非常感谢!
{"error":{"username":["用户名已被占用。"],"email":["邮箱已被占用。"]}}
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];
SBJsonParser * parser = [[SBJsonParser alloc] init];
NSObject * responseobj = [parser objectWithData:data]; // for NSData
NSObject * responseobj = [parser objectWithString:string]; // for NSString
在您的情况下,“responseobj”将是NSDictionary
.