如何在Objective-c中解析此json模式

时间:2016-06-28 13:02:39

标签: php objective-c arrays json multidimensional-array

我有一个寻求电台的iOS应用程序,我想添加燃料价格。

我在数据库中找到了一个Opendata,但是我无法解析数组" Fields"和" price_e10",只有数组"记录"。

这是JSON SCHEMA的一个例子(我认为它的多维数组):

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

这是我在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;

感谢您的帮助

3 个答案:

答案 0 :(得分:1)

我认为数据来自服务器。

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);
}

输出: fuelPrice:1.389

答案 1 :(得分:0)

请按照以下步骤操作:

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

现在String“get_price”的值为1.389,

答案 2 :(得分:0)

就像我上面提到的" Price_e10",您可以直接访问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

        }