gpt4 book ai didi

ios - 在 iOS 中解析 json 并将值添加到对象数组

转载 作者:行者123 更新时间:2023-12-01 17:31:04 31 4
gpt4 key购买 nike

我有以下 json 文件,我正在尝试从我的 iOS 应用程序中解析它。我定义了一个解析文件的方法,但我不知道如何处理整数,即 ID。我想将数据放在一个数组(促销)中,其中包含一个标题和一个产品数组(下面解释得更好)。有什么建议或好的引用吗?

json文件:

 "promotions": {
"1": {
"title": "promotion title",
"product": {
"1": {
"title": "product title",
"description": "this is the description"
},
"2": {
"title": "product title",
"description": "this is the description"
}
}
},
"2": { "3": {
"title": "product title",
"description": "this is the description"
},
"4": {
"title": "product title",
"description": "this is the description"
}
}
}
}

这些是我的数据类:
Promotion { NSString *title; NSArray *products;}
Product { NSString *title; NSString *description;}

我的功能是加载 json 并将所有对象添加到促销数组中,每个促销包含一个产品数组。
- (NSArray *) infoFromJSON{
NSURL *url=[NSURL URLWithString:urlJSON];

NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:30.0];
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

NSMutableArray *promotions = [[NSMutableArray alloc] init];

NSArray *array = [jsonDictionary objectForKey:@"promotions"];
NSLog(@"array: %@", array);
NSLog(@"items en array %d", [array count]);
NSLog(@"object 1 en array: %@", [array objectAtIndex:1]);

// Iterate through the array of dictionaries
for(NSDictionary *dict in array) {
Promotion *promotion= [[Promotion alloc] initWithJSONDictionary:dict];
// Add the promotion object to the array
[promotions addObject:promotions];

//Add the products to each promotion??
}

// Return the array of promotion objects
return promotions;

}

最佳答案

您的 JSON 定义不明确,您必须尝试使用​​以下内容:

[{"promotion_id": 1
"title": "promotion title",
"products": [{"product_id": 1,
"title": "product title",
"description": "this is the description"
},
{"product_id": 2,
"title": "product title",
"description": "this is the description"
},
...
]
},
{"promotion_id": 2
"title": "promotion title",
"products": [{"product_id": 3,
"title": "product title",
"description": "this is the description"
},
{"product_id": 4,
"title": "product title",
"description": "this is the description"
},
...
]
},
...
]

然后,要将 JSON 字典解析为自定义对象,我建议您使用类别 NSObject+Motis我最近一直在工作。将 JSON 字典映射到您的自定义 Objective-C 对象非常有用。

主要是,你必须做到:
@interface Promotion : NSObject
@property (nonatomic, assing) NSInteger promotionId;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSArray *products;
@end

@implementation Promotion
- (NSDictionary*)mjz_motisMapping
{
return @{@"promotion_id" : @"promotionId",
@"title" : @"title",
@"products" : @"products",
};
}

- (NSDictionary*)mjz_arrayClassTypeMappingForAutomaticValidation
{
return @{"products": [Product class]};
}

@end

@interface Product : NSObject
@property (nonatomic, assing) NSInteger productId;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSArray *productDescription;
@end

@implementation Promotion
- (NSDictionary*)mjz_motisMapping
{
return @{@"product_id" : @"productId",
@"title" : @"title",
@"description" : @"productDescription",
};
}
@end

然后通过执行以下操作执行解析:
- (void)parseTest
{
NSData *data = jsonData; // <-- YOUR JSON data

// Converting JSON data into array of dictionaries.
NSError *error = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

if (error)
return; // <--- If error abort.

NSMutableArray *promotions = [NSMutableArray array];
for (NSDictionary *dict in jsonArray)
{
Promotion *promotion = [[Promotion alloc] init];
[promotion mjz_setValuesForKeysWithDictionary:dict];
[promotions addObject:promotion];
}
}

您可以在这篇文章中阅读它的工作原理: http://blog.mobilejazz.cat/ios-using-kvc-to-parse-json

希望它对你有帮助,就像它对我有帮助一样。

关于ios - 在 iOS 中解析 json 并将值添加到对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22359638/

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