简体   繁体   English

在我的Objective-C程序中解析json响应

[英]parsing json response in my objective-c program

im using SBJSON to parse my response, bus somehow i cant get my data out. 我正在使用SBJSON来解析我的响应,我无法以某种方式获取我的数据。 how should i parse this reponed ? 我应该如何解析此回复?

{"StatusCode":0,"Message":"email already exists","Content":{"HasApplication":false,"IsFaceBook":false,"UserActive":false,"UserAuthenticationType":0,"UserCredits":0,"UserDayAdded":0,"UserEmail":null,"UserEmailWasVerified":false,"UserGuid":null,"UserID":0,"UserSellerApproved":false,"UserTokef":0},"TotalCount":0}

i start like this : 我开始是这样的:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{
    [responseData setLength:0];
    NSLog(@"%@",response); 
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    [responseData appendData:data];
    NSLog(@"Data recived");     
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    responseData = nil;

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    NSLog(@"conenction loading finished"); 

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSMutableDictionary *jsonDictionary = [parser objectWithString:responseString error:nil];
}

but whats next ?i need the StatusCode value and the UserID. 但是下一步是什么?我需要StatusCode值和UserID。

One you have the dictionary, you can use it, for example: 一本你有字典,就可以使用它,例如:

NSMutableDictionary *jsonDictionary = [parser objectWithString:responseString error:nil];
NSNumber * statusCode = [jsonDictionary objectForKey:@"StatusCode"];
NSString * message = [jsonDictionary objectForKey:@"Message"];
NSDictionary * content = [jsonDictionary objectForKey:@"Content"];
// etc...

From the looks of the JSON string, you have a Dictionary with three Key Value pairs, one of those being another Dictionary with several Key Value pairs. 从JSON字符串的外观来看,您有一个包含三个键值对的Dictionary,其中之一是另一个具有几个键值对的Dictionary。 So to deal with that once you have assigned the JSON responseString to your NSMutableDictionary: 因此,一旦将JSON responseString分配给NSMutableDictionary,就可以对其进行处理:

Should be something like: 应该是这样的:

NSNumber *statusCode = [jsonDictionary objectForKey:@"StatusCode"];
NSDictionary *contentDict = [jsonDictionary objectForKey@"Content"];
NSString *userID = [contentDict valueForKey@"UserID"];

On a completely different note, if you are going to be doing a lot of web-service interaction, you may want to take a serious look at AFNetworking. 完全不同的是,如果您要进行大量的Web服务交互,则可能需要认真看一下AFNetworking。 It will change your life :) 它会改变你的生活 :)

Also, I can't see why you would need jsonDictionary to be an NSMutableDictionary. 另外,我看不到为什么需要jsonDictionary才能成为NSMutableDictionary。 Unless you are changing it later on, use NSDictionary, it has less overhead. 除非稍后进行更改,否则请使用NSDictionary,它的开销较小。

well..... NSMutableDictionary *jsonDictionary = [parser objectWithString:responseString Maybe it's not NSMutableDictionary, but NSDictionary. NSMutableDictionary *jsonDictionary = [parser objectWithString:responseString ..... NSMutableDictionary *jsonDictionary = [parser objectWithString:responseString也许不是NSMutableDictionary,而是NSDictionary。 and then: 接着:

NSString *statusCode = [jsonDictionary valueForKey:@"StatusCode"];
NSDictionary *contentDict = [jsonDictionary objectForKey@"Content"];
NSString *userID = [contentDict valueForKey@"UserID"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM