Codette
Codette

Reputation: 1007

Objective C parse JSON

I Have this result JSON below that i get from my wcf service which i want to iterate through in my ipad app.I use the code below to parse the result json but the app gives error. How can iterate through this dictionary and get the items.

IOS Code:

   myArray = [NSMutableArray array];
        // Do any additional setup after loading the view.
        NSString* URL = @"http://www.unluapps.com/Service1.svc/ListTopReports";
        NSDictionary *dictionary = [JSONHelper loadJSONDataFromURL:URL ];
        NSString *result=[dictionary valueForKey:@"ListTopReportsResult"];
        NSLog(@"%@",result);

   /* for (NSString* key in result ) {

        id value=[result objectForKey:key];
        NSLog(@"%@ -+",value);

    }
   */

JSON:

{
ListTopReportsResult: [
{
AppUserID: null,
Author: "Joe Matthew",
Company: "Coca-Cola",
Country: "Poland",
CreationDate: "8/20/2014 4:32:00 AM",
EndDate: null,
ResearchReportID: "2",
Sector: "Soft Drinks",
Summary: "da ascon casmld lmasdlasd",
Title: "Can Coca-cola beat the market?",
URL: "2123123.pdf"
},
{
AppUserID: null,
Author: "martina pawlik",
Company: "Cimsa",
Country: "Poland",
CreationDate: "8/20/2014 4:31:00 AM",
EndDate: null,
ResearchReportID: "1",
Sector: "Cement",
Summary: "asd adas asd asdasd asdasdasd",
Title: "Poland's cement sector is on the rise",
URL: "1123123.pdf"
}
]
}

Upvotes: 0

Views: 1099

Answers (2)

Dhrumil
Dhrumil

Reputation: 3204

Try doing something like this. This is not the exact solution and you'll have to work around your way.

NSString *link =  [NSString stringWithFormat:@"yourServiceURL"];
    NSString *encdLink = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url=[NSURL URLWithString:encdLink];
    NSData *response = [NSData dataWithContentsOfURL:url];
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response options: NSJSONReadingMutableContainers error: &error];

Now you have your data in the array and extract it out using objectForKey or valueForKey.You can use NSDictionary or NSArray as per your requirement.

Firstly get the array from the above code. Then extract out the image content like this :

_demoArray = [json valueForKey:@"yourKeyField"];

Now you have the values in your demoArray.

Hope this helps.

Upvotes: 1

Rashad
Rashad

Reputation: 11197

Try this:

NSArray *result=[dictionary valueForKey:@"ListTopReportsResult"];

Because in your JSON, there is an array for key ListTopReportsResult.

Hope this helps. :)

Upvotes: 3

Related Questions