0

我想用 SBJSon libs 解析那些在 iOS 上具有类似结构的 Json 有人可以帮助我吗?非常感谢!

{"error":{"username":["用户名已被占用。"],"email":["邮箱已被占用。"]}}

4

2 回答 2

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];
于 2013-08-16T04:55:31.353 回答
0
SBJsonParser * parser = [[SBJsonParser alloc] init];
NSObject * responseobj = [parser objectWithData:data]; // for NSData
NSObject * responseobj = [parser objectWithString:string]; // for NSString

在您的情况下,“responseobj”将是NSDictionary.

于 2013-08-16T05:48:24.330 回答