2

我是 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";
}
)
4

1 回答 1

1

What you have posted in your question isn't JSON. It might have been originally transmitted as JSON data. But the post shows a data structure in the property list format. Note that some of the strings are HTML or XML encoded which will cause problems later. You should fix this at the source before transmitting it as JSON.

Now the result you post is an array of dictionaries containing further dictionaries. The outmost array is a list of categories. Each category is given as a dictionary containg both its own name and ID as well as the subcategories. The innermost dictionaries are the subcategories containing their ID and name.

The category dictionaries are somewhat problematic because the mix two things and are a mixture of a dictionary and an array. It's difficult to access the subcategories.

Your code could like like this:

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] ];
    }
}
于 2012-07-20T10:56:51.783 回答