我有一个寻找加油站的iOS应用程序,我想添加燃料价格。
我找到了一个Opendata与数据库,但我不能解析数组"字段"one_answers"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;
谢谢你的帮助
我认为数据来自服务器。
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
请按照以下步骤操作:
NSArray *rec = [allDict objectForKey:@"records"];
for (NSDictionary *str in loans) {
NSDictionary *conn = [str objectForKey:@"fields"];
NSStirng *get_price = [conn objectForKey:@"price_e10"];
}
现在字符串"get_price"的值为1.389,
就像上面提到的"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
}