2012-05-06 8 views
0

私の応答を解析するためにSBJSONを使用しています。何とか私のデータを取得できません。 これはどのように解析する必要がありますか?私の目的-cプログラムでjson応答を解析しています

{"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} 

私はこのような開始:

- (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]; 
} 

が、次の私はのStatusCode値とユーザーIDが必要ですいただきました!?。

+0

jsonDictionaryを印刷して、自分で見て、辞書のキーの値/オブジェクトをどのように取得しますか? – 0x8badf00d

答えて

0

一つあなたは辞書を持っている、あなたは例えば、それを使用することができます。

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

JSON文字列のルックスからは、次の3つのキーと値のペアと辞書を持っている、と別の辞書であること、それらの1いくつかのキー値ペア。あなたはWEB-をたくさんやってしようとしている場合は、

完全に別のノートで
NSNumber *statusCode = [jsonDictionary objectForKey:@"StatusCode"]; 
NSDictionary *contentDict = [jsonDictionary [email protected]"Content"]; 
NSString *userID = [contentDict [email protected]"UserID"]; 

のようなものであるべき:あなたはNSMutableDictionaryにJSON responseStringを割り当てた後、だからそれに対処しますサービスのやりとりを行う場合は、AFNetworkingを真剣に検討したいことがあります。あなたの人生を変えるでしょう:)

また、なぜjsonDictionaryをNSMutableDictionaryにする必要があるのか​​分かりません。後でそれを変更しない限り、NSDictionaryを使用するとオーバーヘッドが少なくなります。

0

よく..... NSMutableDictionary *jsonDictionary = [parser objectWithString:responseString 多分NSMutableDictionaryではなくNSDictionaryです。 、次に:

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