变量Objective-C的Json映射
im映射此Json:变量Objective-C的Json映射,objective-c,json,nsstring,nsarray,Objective C,Json,Nsstring,Nsarray,im映射此Json: { "status": "ok", "count": 4, "count_total": 4, "pages": 1, "posts": [ { "id": 220, "type": "products", "slug": "jugo-de-naranja", "url": "http://localhost/?produc
{
"status": "ok",
"count": 4,
"count_total": 4,
"pages": 1,
"posts": [
{
"id": 220,
"type": "products",
"slug": "jugo-de-naranja",
"url": "http://localhost/?products=jugo-de-naranja",
"status": "publish",
"title": "Jugo de Naranja",
"title_plain": "Jugo de Naranja",
"content": "",
"excerpt": "",
"date": "2014-03-12 17:34:01",
"modified": "2014-03-12 17:34:01",
"categories": [
{
"id": 26,
"slug": "juices-2",
"title": "Juices",
"description": "",
"parent": 0,
"post_count": 2
}
],
"tags": [],
"author": {
"id": 1,
"slug": "arturocalvo",
"name": "arturocalvo",
"first_name": "",
"last_name": "",
"nickname": "arturocalvo",
"url": "",
"description": ""
},
"comments": [],
"attachments": [],
"comment_count": 0,
"comment_status": "closed",
"custom_fields": {
"product_type": [
"bebida"
],
"tipo_de_bebida": [
"jugo"
],
"jugo_0_size": [
"m"
],
"jugo_0_real_id": [
"22"
],
"jugo_0_price": [
"20"
],
"jugo_0_sucursal": [
"2"
],
"jugo": [
"1"
]
}
}
}
但我的问题是,我需要为这个产品类在自定义字段部分映射tipo_de_bebida
@interface Products : NSObject
@property (nonatomic, copy) NSSet * productType;
@property (nonatomic, copy) NSString * name;
@property (nonatomic, copy) NSSet * productCategory;
@property (nonatomic, copy) NSSet * locationId;
@property (nonatomic, copy) NSSet * realId;
@property (nonatomic, copy) NSSet * price;
@property (nonatomic, copy) NSSet * size;
@property (nonatomic, copy) NSSet * mod;
@property (nonatomic, copy) NSSet * temp;
映射将与产品类型一起进行,因为我需要映射所有其他属性,但我的问题是我需要映射同一类中的其他产品,但映射其他产品字段
"product_type": [
"bebida"
],
"tipo_de_bebida": [
"jugo"
名称不同,因此在字段中
jugo_0_尺寸
jugo_0_real_id
jugo_0_价格
你成功了吗
“jugo”部分改为“platillo”或“cafe”,我尝试在数组中提取json以获得具有不同文本的字符串,但我的部分似乎不起作用,下面是代码
NSData* jsonData = [NSData dataWithContentsOfURL:
baseURL];
NSError* error;
NSDictionary* productJson = [NSJSONSerialization
JSONObjectWithData:jsonData
options:kNilOptions
error:&error];
NSMutableArray *productArray = [[NSMutableArray alloc]init];
for(NSString * key in productJson){
[productArray addObject:key];
}
有人能告诉我我做错了什么吗?提前感谢您是否正在尝试将字典的键或对象放入数组中。因为在这两种情况下,for循环都是错误的。假设JSON的缩进是正确的,“tipo_de_bebida”是“custom_fields”对象(字典)中的一个元素,该对象(字典)是“posts”数组的元素。上面的代码会将“status”、“count”、“count_total”、“pages”和“posts”键添加到“productArray”。这是您真正想要的吗?提示:一步一个脚印。访问
productJson
获取“posts”,这将是一个NSArray。遍历数组,其中每个元素将是一个NSDictionary,包含“id”、“type”、“categories”…“自定义字段”“.NSLog你有什么,以确保你在你认为你应该在的地方。使用每个数组条目引用字典并提取所需的数据。