2011-12-07 11 views
0

私は以下のjsonメソッドと次のコードを使用してjsonメソッドを解析し、必要なデータをラベルに表示しています。 私はこのリンクhttp://www.touch-code-magazine.com/tutorial-fetch-and-parse-json/に従っていますが、他の多くの人が結果を得ていません。コード行の下に例外がスローされるか、それ以外の場合はnull値が表示されます。は、jsonの解析とデータの表示に問題があります。

NSDictionary* profile = [profileinfo objectAtIndex:0]; //throws exception 

誰もが以下のコードで間違っているものを私を助けることができるとどのようなので、私は値すなわち、PHONENUMBER、JSON方法からFirstNameおよびその他のデータを取得するTATがありません。

//Json Method 

{ 

"createdBy":"superadmin", 
"createdOn":"2011-11-15T00:49:06+05:30", 
"updatedBy":"superadmin", 
"updatedOn":"2011-11-15T00:49:06+05:30", 
"contactNumber":"9945614074", 
"emailNotification":"true", 
"firstName":"resident2", 
"lastName":"user5", 
"loginId":"jin", 
"married":"false", 
"message":"", 
"preferredLanguage":"ko_KR", 
"sex":"0", 
"smsNotification":"false", 
"status":"ACTIVE", 
"subscribedPlans":"Intelligent Concierge", 
"userName":"Cisco" 

} 

//code 

- (void)loadData 

{ 

dataWebService = [[NSMutableData data] retain]; 


NSURLRequest *request = [[NSURLRequest requestWithURL:[NSURL URLWithString:@"URL LINK"]]retain]; 

[[NSURLConnection alloc]initWithRequest:request delegate:self]; 

} 


-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 


[connection release];  
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding]; 

self.dataWebService = nil; 

NSArray* profileinfo = [(NSDictionary*) [responseString JSONValue] objectForKey:@"createdBy"]; 
[responseString release];  

NSDictionary* profile = [profileinfo objectAtIndex:0]; 

//fetch the data 
NSNumber* numb = [profile objectForKey:@"contactNumber"]; 

NSString* name = [profile objectForKey:@"firstName"]; 

//set the text to the label 
label.numberOfLines = 0; 
label.text = [NSString stringWithFormat:@"contactNumber: %@ \n \n Name: %@ \n \n", 
      numb,name]; 

} 

答えて

1

jsonValueはあなたの辞書です。

NSDictionary * profile = (NSDictionary*)[responseString JSONValue]; 

NSArray* profileinfo = [(NSDictionary*) [responseString JSONValue] objectForKey:@"createdBy"]; 

NSDictionary* profile = [profileinfo objectAtIndex:0]; 

を交換し、今では自分の価値観

+0

がOK NSDictionaryのを使用する必要*プロフィール=を取得しないようにobjectForKey使用[ profileinfo objectAtIndex:0];この行コード?? – crazy2431

+0

いいえ、NSDictionary * profile =(NSDictionary *)[responseString JSONValue]を使用しないでください。 – user1084563

+0

jsonの{}は、jsonvalueがnsdictionaryであることを意味します。あなたのコードは{"createdBy":[{}]} – user1084563

関連する問題