2012-07-20 5 views
1

내 JSON 파일이인쇄 레이블 텍스트

[{"id":"1","category":"New Category1","title":"New Product2","description":"Please type a description1","imgurl":"http:\/\/s.wordpress.org\/about\/images\/wordpress-logo-notext-bg.png","spectitle":"Specification","specvalue":"Value"},{"id":"2","category":"Mobile","title":"Samsung","description":"Please type a description","imgurl":"http:\/\/s.wordpress.org\/about\/images\/wordpress-logo-notext-bg.png","spectitle":"Price","specvalue":"100$"}] 

모양과 내가 성공적으로 전체를 분석 한 개별 labels.I의 ID, 카테고리, 제목, 설명을 인쇄 할 수있는 4 레이블이 json 개체를 하나의 레이블에 인쇄 할 수 있습니다. 해당하는 범주에 해당하는 모든 json 개체를 해당 레이블에 표시해야합니다. 나는 개인의 가치를하려고하면

NSString *labelstring =[NSString stringWithFormat:@"%@",[json objectAtIndex:0]]; 
label1.text=[NSString stringwithformat: objectForKey:@"id"]; 
label2.text=[NSString stringwithformat:objectForKey:@"category"]; 
label3.text=[NSString stringwithformat:objectForKey:@"title"]; 
label4.text=[NSString stringwithformat:objectForKey:@"description"]; 

여기에 JSON이 URL 정보와 그 일을 함께 NSArray를위한 변수입니다 ...하지만 난

+0

가 어떻게'json' 변수를 할당 할을? json 파서를 사용합니까? 어느 것? – Adam

+0

그 stringWithFormat : 호출은 불필요합니다 ... 당신은 몰랐습니까? –

답변

4

이 있습니까 ... it..Guidance을하시기 바랍니다받을 수 없습니다 너 진짜 코드 보이니? [NSString stringwithformat:objectForKey:@"id"]; 구문이 잘못되었습니다. 어떤 오류가 발생하지 않았습니까?

올바른 코드 :

// ??? NSString *labelstring = [NSString stringWithFormat:@"%@",[json objectAtIndex:0]]; 
NSDictionary *dict = [json objectAtIndex:0]; 
label1.text = [dict valueForKey:@"id"]; 
label2.text = [dict valueForKey:@"category"]; 
label3.text = [dict valueForKey:@"title"]; 
label4.text = [dict valueForKey:@"description"]; 

편집 : 당신이이다, 그래서 여기에 올바른 방법으로 JSON을 구문 분석하는 경우 나도 몰라, 너무 :

NSData *jsonData = ... 
NSArray *json = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil]; 
2

json을이 방법으로 사용하려면 NSDictionary으로 디코딩해야하며 후반부에 시도하는 방식대로 사용할 수 있습니다.

,
label1.text = [NSString stringwithFormat:@"%@", [json objectForKey:@"id"]]; 

또는 당신은 단순히 당신이 할 수있는 JSON에서 "id" 태그 아래에있는 텍스트가 필요한 경우 :

label1.text = [json objectForKey:@"id"]; 
5
(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://advantixcrm.com/prj/mitech/index.php/api/appconfig/Mg"]]; 

    [request setHTTPMethod:@"GET"]; 

    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"]; 

    NSError *err; 
    NSURLResponse *response; 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 

    NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err]; 

    NSArray *array=[jsonArray objectForKey:@"info"]; 

    for (int i=0; i<[array count]; i++) { 
     _label.text = [[array objectAtIndex:i]objectForKey:@"restaurant_location"]); 
    } 
}