gpt4 book ai didi

iphone - JSON数据问题iPhone

转载 作者:行者123 更新时间:2023-11-28 23:08:25 26 4
gpt4 key购买 nike

我从 JSON 网络服务获取这些数据

[{"identity":"DEMO","assets":[{"identity":"34DL3611","systemId":"544507"},{"identity":"34GF0512","systemId":"5290211"},{"identity":"34HH1734","systemId":"111463609"},{"identity":"34HH1736","systemId":"111463622"},{"identity":"34YCJ15","systemId":"294151155"}],"systemId":4921244}]

我正在使用此方法获取身份 Assets 的值("assets":[{"identity":"34DL3611","systemId":"544507"}):

 vehicleList = [dict objectForKey: @"assets"];

self.listVehicles = [[NSMutableArray alloc] init];

for (NSUInteger index = 0; index < [vehicleList count]; index++) {

itemDict = [vehicleList objectAtIndex: index];

[self.listVehicles addObject:[itemDict objectForKey:@"identity"]];
}

如何获取 systemId 值...?

我已经为 systemId 试过了

vehicleListID = [dict objectForKey:@"systemId"];

self.listVehiclesID =[[NSMutableArray alloc]init];

for (NSUInteger index = 0; index < [vehicleListID count]; index++) {

assetsIdDict = [vehicleListID objectAtIndex: index];

[self.listVehiclesID addObject:[assetsIdDict objectForKey:@"systemId"]];
}

但出现错误:[__NSCFNumber count]:无法识别的选择器发送到实例 0x784c730

最佳答案

您基本上就快完成了。您可能只想要这个:

NSString *systemId = [itemDict objectForKey:@"systemId"];

关于这段代码,你有:

vehicleListID = [dict objectForKey:@"systemId"];

self.listVehiclesID =[[NSMutableArray alloc]init];

for (NSUInteger index = 0; index < [vehicleListID count]; index++) {

assetsIdDict = [vehicleListID objectAtIndex: index];

[self.listVehiclesID addObject:[assetsIdDict objectForKey:@"systemId"]];
}

... 好吧,那是完全错误的。这将返回 NSNumber,在您的示例 JSON 中,它是 4921244 就在 JSON 字符串的末尾。然后您在其上调用 count,但它不是数组,因此它会崩溃。

如果你想从那个 JSON 中获取所有的值,你可以使用这个:

NSNumber *outerIdentity = [dict objectForKey:@"identity"];
NSNumber *outerSystemId = [dict objectForKey:@"systemId"];
NSArray *vehicleList = [dict objectForKey:@"assets"];
for (NSUInteger index = 0; index < [vehicleList count]; index++) {
NSDictionary *itemDict = [vehicleList objectAtIndex: index];
NSString *identity = [itemDict objectForKey:@"identity"];
NSString *systemId = [itemDict objectForKey:@"systemId"];
}

然后对所有这些对象做任何您想做的事。

关于iphone - JSON数据问题iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8850587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com