gpt4 book ai didi

objective-c - 从 json 数组创建一个强类型对象

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

我有一个我填充的数组,我想依次将其放置在强类型对象中。我有这段代码,其中填充了 json 数据,我想用这些数据填充我的设置对象。我该怎么做?

JSON 响应:

得到的回应是:

[
{
"CatalogID":2377,
"Category":"Frozen",
"Facings":true,
"ID":13,
"LastUpdateDateTime":"\/Date(1348681365520-0700)\/",
"Quantity":true
},
{
"CatalogID":2377,
"Category":"Fruit",
"Facings":true,
"ID":10,
"LastUpdateDateTime":"\/Date(1348692069843-0700)\/",
"Quantity":false
},
{
"CatalogID":2377,
"Category":"Salads",
"Facings":true,
"ID":12,
"LastUpdateDateTime":"\/Date(1348681354807-0700)\/",
"Quantity":true
},
{
"CatalogID":2377,
"Category":"Vegetables",
"Facings":true,
"ID":11,
"LastUpdateDateTime":"\/Date(1348681334523-0700)\/",
"Quantity":true
}
]

Objective-C

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {

NSError *error = nil;
// Get the JSON data from the website

id result = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];

NSStringEncoding responseEncoding = NSUTF8StringEncoding;
if ([response textEncodingName]) {
CFStringEncoding cfStringEncoding = CFStringConvertIANACharSetNameToEncoding((__bridge CFStringRef)[response textEncodingName]);
if (cfStringEncoding != kCFStringEncodingInvalidId) {
responseEncoding = CFStringConvertEncodingToNSStringEncoding(cfStringEncoding);
}
}

if(receivedData)
{
NSString *dataString = [[NSString alloc] initWithData:receivedData encoding:responseEncoding];

NSLog(@"Got response as %@", dataString);

}

if ([result isKindOfClass:[NSArray class]]) {

for (NSArray *item in result) {
NSArray *items = [item valueForKey:@"ID"];

[_settings addObject:items];

// Try to populate Setting object
Setting *setting = (Setting *)[_settings objectAtIndex:0];

NSLog(@"Setting: %@", setting);
}
}
else {
NSDictionary *jsonDictionary = (NSDictionary *)result;

for(NSDictionary *item in jsonDictionary)
NSLog(@"Item: %@", item);
}

NSLog(@"Finished");
}

设置.h

@interface Setting : NSObject {

NSString *_settingsID;
NSString *_catalogID;
NSString *_category;
NSString *_facings;
NSString *_quantity;
}

@property (nonatomic, retain) NSString *settingsID;
@property (nonatomic, retain) NSString *catalogID;
@property (nonatomic, retain) NSString *category;
@property (nonatomic, retain) NSString *facings;
@property (nonatomic, retain) NSString *quanity;

- (Setting *)initWithName:(NSString *)settingsID desc:(NSString *)category;

@end

设置.m

@implementation Setting

@synthesize settingsID = _settingsID;
@synthesize catalogID = _catalogID;
@synthesize category = _category;
@synthesize facings = _facings;
@synthesize quanity = _quantity;

- (Setting *)initWithName:(NSString *)settingsID desc:(NSString *)category CategoryID:(NSString *)catalogID Facings:(NSString *)facings Quantity:(NSString *)quantity {

if ((self = [super init])) {

self.settingsID = settingsID;
self.catalogID = catalogID;
self.category = category;
self.facings = facings;
self.quanity = quantity;
}

return self;

}

@end

最佳答案

您发布的代码中有不少错误。我将遍历它们并向您展示如何正确地做到这一点,但我强烈建议您研究 NSArray、NSDictionary 和 NSString 的文档,以熟悉这些类中可用的方法——您真的不能甚至在不太了解这些类的情况下就开始编写像样的代码。

1) 我真的不知道你想用字符串编码的东西和 if(receivedData) 之后的代码做什么。无需将您的 JSON 转换为字符串,JSONObjectWithData: 方法会将您的数据转换为一个数组(更准确地说,一个字典数组),而这正是您需要使用的。

2) 在你的 for-in 循环中,item 应该被输入为一个 NSDictionary,而不是一个数组。数组中的每一项都是您发布的 JSON 中 {} 之间的整个字典。

3) 你在 for-in 循环中的所有代码都是错误的——我不知道你想用它做什么。您需要做的是获取字典中各种键的值,分配初始化 Setting 的实例,并在该扩展初始化方法中传递值。

4) 您的设置对象只是在您的 JSON 数据中保留其中一个词典的值,因此您需要创建另一个数组来为您的响应中的每个词典保存一个设置对象(我在下面的示例中将其称为 settingsArray) .

5) 您在 Setting 类中的 init 方法在 .h 和 .m 中是不同的,它们应该看起来都像 .m 版本,除了每个冒号后面的名称部分应该是小写而不是大写。

所以,这是我认为可行的代码。由于我无权访问您的 JSON,因此无法对其进行测试,因此请尝试一下,然后将结果返回给我。

因此,在您进行下载的类中,您应该在 connectionDidFinishLoading 方法中有一个属性 settingsArray 和以下代码:

-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
self.settingsArray = [NSMutableArray array];
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&error];
if ([result isKindOfClass:[NSArray class]]) {

for (NSDictionary *item in result) {
NSString *settingsID = [item objectForKey:@"ID"];
NSString *category = [item objectForKey:@"Category"];
NSString *categoryID = [item objectForKey:@"CatalogID"];
NSString *facings = [item objectForKey:@"Facings"];
NSString *quantity = [item objectForKey:@"Quantity"];

Setting *setting = [[Setting alloc] initWithName:settingsID desc:category categoryID:categoryID facings:facings quantity:quantity];
[self.settingsArray addObject:setting];
}

}
}

Setting 中的 init 方法应该是这样的:

- (Setting *)initWithName:(NSString *)settingsID desc:(NSString *)category categoryID:(NSString *)catalogID facings:(NSString *)facings quantity:(NSString *)quantity {

if ((self = [super init])) {

self.settingsID = settingsID;
self.catalogID = catalogID;
self.category = category;
self.facings = facings;
self.quanity = quantity;
}

return self;

}

编辑后:如果你想根据另一个属性的值找到设置的一个属性的值,你可以这样做:

    for (Setting *aSetting in _settingsArray) {
if ([aSetting.category isEqualToString:@"Frozen"]) {
NSLog(@"%@",aSetting.facings);
}
}

关于objective-c - 从 json 数组创建一个强类型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12631905/

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