Objective-C/JSON,字典数组,字典中嵌套的解析数组
我正在解析的JSON可以在这里找到“编辑” 除了通知和设备之外,我可以正确地从这个JSON抓取所有对象。 我在获取包含“deviceId”的设备字典数组时遇到了很多麻烦。我现在的代码如下所示Objective-C/JSON,字典数组,字典中嵌套的解析数组,objective-c,json,parsing,nsarray,nested,Objective C,Json,Parsing,Nsarray,Nested,我正在解析的JSON可以在这里找到“编辑” 除了通知和设备之外,我可以正确地从这个JSON抓取所有对象。 我在获取包含“deviceId”的设备字典数组时遇到了很多麻烦。我现在的代码如下所示 dispatch_async(kBgQueue, ^{ NSData* data = [NSData dataWithContentsOfURL: iViuListOfCustomersURL]; [self performSelectorOnMai
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
iViuListOfCustomersURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:NO];
});
- (void) fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json;
json = [NSJSONSerialization
JSONObjectWithData:responseData // 1
options:kNilOptions
error:&error];
NSArray* customers = json;
for(int i = 0; i < customers.count ; i++){
NSDictionary *customer = [customers objectAtIndex:i];
cmsServer = [customer objectForKey:@"cmsServer"];
iconUrl = [NSURL URLWithString:[customer objectForKey:@"iconUrl"]];
wideLogo = [customer objectForKey:@"wideLogo"];
customerNo = [NSNumber numberWithInt:[[customer objectForKey:@"customerNo"] intValue]];
placeName = [customer objectForKey:@"placeName"];
distance = [NSNumber numberWithFloat:[[customer objectForKey:@"distance"] floatValue]];
placeId = [NSNumber numberWithInt:[[customer objectForKey:@"placeId"] intValue]];
address = [customer objectForKey:@"address"];
cmsName = [customer objectForKey:@"cmsName"];
hasLocations = [NSNumber numberWithInt:[[customer objectForKey:@"hasLocations"]intValue]];
isFavorite = [NSNumber numberWithInt:[[customer objectForKey:@"isFavorite"] intValue]];
longitude = [NSNumber numberWithFloat:[[customer objectForKey:@"longitude"] floatValue]];
latitude = [NSNumber numberWithFloat:[[customer objectForKey:@"latitude"] floatValue]];
hasArtifacts = [NSNumber numberWithInt:[[customer objectForKey:@"hasArtifacts"]intValue]];
// I have tried a number of things and this was what I had for the last try.
devices = [NSArray arrayWithObject:[customer objectForKey:@"devices"]];
NSLog(@"%@",devices.count);
}
dispatch\u async(kBgQueue^{
NSData*data=[NSData data WITH CONTENTS URL:
IviulistofCustomerssurl];
[self-performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:NO];
});
-(void)获取数据:(NSData*)响应数据{
n错误*错误;
NSDictionary*json;
json=[NSJSONSerialization
JSONObjectWithData:responseData//1
选项:针织品
错误:&错误];
NSArray*customers=json;
对于(int i=0;i
}
我对objective-c比较陌生,想自己解决这个问题,但我在这个问题上花了太多时间,没有这些设备ID我无法继续
谢谢大家! 我不知道这是否会有所不同,但您是否尝试过:
devices = [NSArray arrayWithArray:[customer objectForKey:@"devices"]];
您可以尝试的另一件事是浏览其中的元素并将它们逐个添加到设备中:
for(DeviceObject *deviceObject in [customer objectForKey:@"devices"])
{
[devices addObject:deviceObject];
}
您还可以查看json中有时传递的空值。我不知道这是否会产生影响,但您是否尝试过:
devices = [NSArray arrayWithArray:[customer objectForKey:@"devices"]];
您可以尝试的另一件事是浏览其中的元素并将它们逐个添加到设备中:
for(DeviceObject *deviceObject in [customer objectForKey:@"devices"])
{
[devices addObject:deviceObject];
}
另外,您可以查看json中有时传递的空值