Alex - Exaland Concept
Alex - Exaland Concept

Reputation: 127

How can I parse this json schema in Objective-c

I have an iOS app that seek station , and I would like add the Fuel price.

I found an Opendata with the database, but I can't parse the array "Fields" and "price_e10", only the array "Records".

Here's an example of JSON SCHEMA (I think it's multidimensional-array):

"records":[
{
"datasetid":"prix_des_carburants_j_7",
"recordid":"fa74ca1fdf6938333d2bc1013623b66771557b31",
"fields":{
"price_e10":1.389,

Here the example of my code in Objective-c :

NSError *e;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &e];

    NSArray *arrayResult =[dict objectForKey:@"records"];
    arraySmpl = [NSMutableArray arrayWithArray:arrayResult];

    NSLog(@" MULTIPLE ARRAY : %@ ",arrayResult);
    rowsInSection = [arraySmpl count] + 1;

Thanks for your help

Upvotes: 0

Views: 127

Answers (3)

Muthukumar
Muthukumar

Reputation: 62

Like i above mentioned for "Price_e10", you can directly access price_gazole price_sp95 , price_sp98.

NSArray *rec = [allDict objectForKey:@"records"];  
         for (NSDictionary *str in loans) {
         NSDictionary *conn = [str objectForKey:@"fields"];
                NSStirng *get_price = [conn objectForKey:@"price_e10"];
 NSStirng *price_gazole = [conn objectForKey:@"price_gazole"]; //output: 1.205
 NSStirng *price_sp95 = [conn objectForKey:@"price_sp95"];  //output :1.425

 NSStirng *price_sp98 = [conn objectForKey:@"price_sp98"]; //output 1.465

        }

Upvotes: 0

Ramesh_T
Ramesh_T

Reputation: 1261

I considered data is coming from server.

NSDictionary *globalDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSArray *recordsArray = [globalDict valueForKey:@"records"];
for (NSDictionary *dict in recordsArray) {
    NSDictionary *fieldsDict = [dict valueForKey:@"fields"];
    NSString *fuelPrice = [fieldsDict valueForKey:@"price_e10"];
    NSLog(@"fuelPrice: %@", fuelPrice);
}

Output : fuelPrice: 1.389

Upvotes: 1

Muthukumar
Muthukumar

Reputation: 62

Please follow the below steps:

 NSArray *rec = [allDict objectForKey:@"records"];  
     for (NSDictionary *str in loans) {
     NSDictionary *conn = [str objectForKey:@"fields"];
            NSStirng *get_price = [conn objectForKey:@"price_e10"];
    }

now the String "get_price" will have the value of 1.389,

Upvotes: 0

Related Questions