我是ios开发的新手.. 我在从复杂的json响应中获取数组时遇到问题。 所以任何人都有这个想法,请帮助我... 我的json回复如下 我想获得subcat_id和subcat_name
的数组 (
{
0 = {
"subcat_id" = 2;
"subcat_name" = Restaurants;
};
1 = {
"subcat_id" = 3;
"subcat_name" = "Bar & Club";
};
"cat_id" = 1;
"cat_name" = "Dining & Nightlife";
},
{
0 = {
"subcat_id" = 5;
"subcat_name" = Massage;
};
1 = {
"subcat_id" = 6;
"subcat_name" = Facial;
};
2 = {
"subcat_id" = 7;
"subcat_name" = "Manicure / Pedicure";
};
3 = {
"subcat_id" = 8;
"subcat_name" = Tanning;
};
4 = {
"subcat_id" = 9;
"subcat_name" = "Hair Salon";
};
5 = {
"subcat_id" = 10;
"subcat_name" = "Hair Removal";
};
6 = {
"subcat_id" = 11;
"subcat_name" = Spa;
};
7 = {
"subcat_id" = 12;
"subcat_name" = "Teeth Whitening";
};
8 = {
"subcat_id" = 13;
"subcat_name" = "Eye & Vision";
};
9 = {
"subcat_id" = 15;
"subcat_name" = "Cosmetic & Beauty Treatments";
};
"cat_id" = 4;
"cat_name" = "Health & Beauty";
},
{
0 = {
"subcat_id" = 17;
"subcat_name" = Pilates;
};
1 = {
"subcat_id" = 18;
"subcat_name" = Yoga;
};
2 = {
"subcat_id" = 19;
"subcat_name" = Gym;
};
3 = {
"subcat_id" = 20;
"subcat_name" = "Boot Camp";
};
"cat_id" = 16;
"cat_name" = Fitness;
},
{
0 = {
"subcat_id" = 22;
"subcat_name" = "Men's Clothing";
};
1 = {
"subcat_id" = 23;
"subcat_name" = "Women's Clothing";
};
2 = {
"subcat_id" = 24;
"subcat_name" = "Food & Grocery";
};
3 = {
"subcat_id" = 25;
"subcat_name" = "Wine & Liquor";
};
4 = {
"subcat_id" = 26;
"subcat_name" = "Home Services";
};
5 = {
"subcat_id" = 27;
"subcat_name" = "Rental Car/ Car Wash/ Car Repair";
};
6 = {
"subcat_id" = 53;
"subcat_name" = "Product / Service Discounts";
};
"cat_id" = 21;
"cat_name" = "Retail & Services";
},
{
0 = {
"subcat_id" = 29;
"subcat_name" = Museums;
};
1 = {
"subcat_id" = 30;
"subcat_name" = "Wine Tasting";
};
10 = {
"subcat_id" = 42;
"subcat_name" = "Dance Classes";
};
11 = {
"subcat_id" = 50;
"subcat_name" = "Family Fun";
};
2 = {
"subcat_id" = 31;
"subcat_name" = "City Tours";
};
3 = {
"subcat_id" = 32;
"subcat_name" = "Comedy Clubs";
};
4 = {
"subcat_id" = 33;
"subcat_name" = Theater;
};
5 = {
"subcat_id" = 34;
"subcat_name" = Concerts;
};
6 = {
"subcat_id" = 35;
"subcat_name" = "Education & Personal Development";
};
7 = {
"subcat_id" = 36;
"subcat_name" = Golf;
};
8 = {
"subcat_id" = 38;
"subcat_name" = Bowling;
};
9 = {
"subcat_id" = 39;
"subcat_name" = "Sporting Events";
};
"cat_id" = 28;
"cat_name" = "Activities & Adventures";
},
{
0 = {
"subcat_id" = 44;
"subcat_name" = Others;
};
1 = {
"subcat_id" = 56;
"subcat_name" = "Accommodation and Holidays";
};
"cat_id" = 43;
"cat_name" = Others;
},
{
0 = {
"subcat_id" = 46;
"subcat_name" = "Pet Grooming Services";
};
"cat_id" = 45;
"cat_name" = Pets;
},
{
0 = {
"subcat_id" = 52;
"subcat_name" = "Car Servicing / Repairs";
};
"cat_id" = 51;
"cat_name" = "Car Servicing / Repairs";
}
)
答案 0 :(得分:1)
您在问题中发布的内容不是JSON。它最初可能是作为JSON数据传输的。但帖子显示了属性列表格式的数据结构。请注意,某些字符串是HTML或XML编码的,这将导致以后出现问题。在将其作为JSON传输之前,您应该在源处修复此问题。
现在发布的结果是包含更多字典的字典数组。最外面的数组是一个类别列表。每个类别都作为包含其自己的名称和ID以及子类别的字典给出。最里面的词典是包含其ID和名称的子类别。
类别词典有些问题,因为它混合了两个东西,并且是字典和数组的混合。访问子类别很困难。
您的代码可能是这样的:
NSArray* categoryList = [someJSONParser parse];
for (NSDictionary* category in categoryList ) {
NSString* categoryName = [category objectForKey: @"cat_name"];
NSNumber* categoryIdObj = [category objectForKey: @"cat_id"];
int categoryId = [categoryIdObj intValue];
int index = 0;
NSDictionary* subcategory = [category objectForKey: [NSString stringWithFormat: @"%d", index] ];
while (subcategory != nil) {
NSString* subcategoryName = [subcategory objectForKey: @"subcat_name"];
NSNumber* subcategoryIdObj = [subcategory objectForKey: @"subcat_id"];
int subcategoryId = [subcategoryIdObj intValue];
index++;
subcategory = [category objectForKey: [NSString stringWithFormat: @"%d", index] ];
}
}