Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 改为;“匿名”;xcode中的json_Iphone_Ios - Fatal编程技术网

Iphone 改为;“匿名”;xcode中的json

Iphone 改为;“匿名”;xcode中的json,iphone,ios,Iphone,Ios,通常我会阅读包装在“数据”顶级项中的json。但是在下面的例子中,没有顶级的“数据”项 通常我会这样做 NSDictionary *temp = [[MyArray1 objectAtIndex:0]valueForKey:@"Data"]; 但这在这种情况下是行不通的。在像这样的匿名JSON对象的情况下,如何循环遍历元素?1。)首先使用 jsonArray = [NSJSONSerialization JSONObjectWithData:data

通常我会阅读包装在“数据”顶级项中的json。但是在下面的例子中,没有顶级的“数据”项

通常我会这样做

NSDictionary *temp = [[MyArray1 objectAtIndex:0]valueForKey:@"Data"];
但这在这种情况下是行不通的。在像这样的匿名JSON对象的情况下,如何循环遍历元素?

1。)首先使用

 jsonArray = [NSJSONSerialization JSONObjectWithData:data
                                                               options:0
                                                                 error:&error];
2.)然后循环此数组中的所有对象

for(NSMutableDictionary *dictionary in jsonArray){
 NSString *name=[dictionary objectForKey:@"AreaName"];
 //and so on.
}

在您的例子中,json不是一个字典,而是一个数组

NSMutableArray *response = [[NSString stringWithContentsOfURL:[NSURL URLWithString:requestUrlString] encoding:NSUTF8StringEncoding error:NULL] JSONValue];

for(NSDictionary *dictionary in response)
{
     NSLog(@"Data Dictionary is : %@",dictionary);
}
试一试


首先,您必须从下载JSON框架

并在类的顶部导入JSON.h。然后执行以下代码:

NSArray *arr = [responseString JSONValue];

    NSLog(@"JSOn arr count : %d",[arr count]);

    for (int i =0;i< [arr count]; i++) {

    NSDictionary *dicObj = [arr objectAtIndex:i];

    NSString *strAreaName = (NSString*) [dicObj objectForKey:@"AreaName"];
    int areaID = [dicObj objectForKey:@"AreaId"];
    NSString *strDestinationName = (NSString*) [dicObj objectForKey:@"DestinationName"];
int destID = [dicObj objectForKey:@"DestinationId"];
    NSString *strCountryName = (NSString*) [dicObj objectForKey:@"CountryName"];
    int countryID = [dicObj objectForKey:@"CountryId"];

    }
NSArray*arr=[responseString JSONValue];
NSLog(@“JSOn arr计数:%d”,[arr计数];
对于(int i=0;i<[arr count];i++){
NSDictionary*dicObj=[arr objectAtIndex:i];
NSString*strAreaName=(NSString*)[dicObj objectForKey:@“AreaName]”;
int areaID=[dicObj objectForKey:@“areaID”];
NSString*strDestinationName=(NSString*)[dicObj objectForKey:@“DestinationName]”;
int destID=[dicObj objectForKey:@“DestinationId”];
NSString*strCountryName=(NSString*)[dicObj objectForKey:@“CountryName]”;
int countryID=[dicObj objectForKey:@“countryID”];
}

希望有帮助。

您显示的输出既不是字典也不是数组。显示
temp
prints列表是由两个字典组成的数组。没什么复杂的。@HotLicks:我是通过看[和]说的。我只是想评论一下,我真的不明白为什么这个问题受到如此多的嘲笑或被关闭了。这是一个合理的问题,OP可能对这一问题提出了错误的看法。您是否会使用
NSMutableArray
来读取匿名JSON数组并不明显(至少,对于我们这些不经常使用JSON的人来说,我们不这样认为,他们习惯于只查看与JSON相关的
NSDictionary
)。这个问题和相关的答案对我很有用。如果它解决了你的问题,那么你可以接受它作为答案。
//JSON data from webservice
id json = ...
NSArray *areas = nil;
if ([json isKindOfClass:[NSDictionary class]]) {

   areas = json[@"Data"];

}else if ([json isKindOfClass:[NSArray class]])
{
    areas = json;
}

[areas enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
     //Enumerate
}];
NSArray *arr = [responseString JSONValue];

    NSLog(@"JSOn arr count : %d",[arr count]);

    for (int i =0;i< [arr count]; i++) {

    NSDictionary *dicObj = [arr objectAtIndex:i];

    NSString *strAreaName = (NSString*) [dicObj objectForKey:@"AreaName"];
    int areaID = [dicObj objectForKey:@"AreaId"];
    NSString *strDestinationName = (NSString*) [dicObj objectForKey:@"DestinationName"];
int destID = [dicObj objectForKey:@"DestinationId"];
    NSString *strCountryName = (NSString*) [dicObj objectForKey:@"CountryName"];
    int countryID = [dicObj objectForKey:@"CountryId"];

    }