Vishnu
Vishnu

Reputation: 2243

Print Label text from NSDictionary which have JSON Object

my json file looks like this

[{"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$"}]

and i have 4 label that have to print id,category,title,description in individual labels.I have successfully parsed entire json object and printed in a single label.I need to display all the json Object with corresponding category to corresponding label.

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"]; 

here json is a variable for NSArray with have URL info and those things...But if i try to get individual value i can't get it..Guidance please...

Upvotes: 1

Views: 4908

Answers (3)

Avinash Mishra
Avinash Mishra

Reputation: 797

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

Upvotes: 5

akashivskyy
akashivskyy

Reputation: 45180

Are you showing your real code? [NSString stringwithformat:objectForKey:@"id"]; is invalid syntax. Don't you get any errors?

The correct code:

// ??? 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"];

Edit: I don't know if you parse your JSON in the correct way, so here is it, too:

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

Upvotes: 4

Dan F
Dan F

Reputation: 17732

If you want to use the json that way, you need to decode it into an NSDictionary then you can use it the way you're trying to in the latter half:

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

Or if you simply need the text that is under the "id" tag in json you can do:

label1.text = [json objectForKey:@"id"];

Upvotes: 2

Related Questions