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

質問に投稿したのはJSONではありません。元々はJSONデータとして送信された可能性があります。ただし、投稿には、プロパティリスト形式のデータ構造が示されています。一部の文字列はHTMLまたはXMLでエンコードされているため、後で問題が発生することに注意してください。JSONとして送信する前に、ソースでこれを修正する必要があります。

これで、投稿した結果は、さらに辞書を含む辞書の配列になります。最も外側の配列はカテゴリのリストです。各カテゴリは、サブカテゴリだけでなく、独自の名前とIDの両方を含む辞書として提供されます。最も内側の辞書は、IDと名前を含むサブカテゴリです。

カテゴリ辞書は、2つのものが混在していて、辞書と配列が混在しているため、やや問題があります。サブカテゴリにアクセスするのは困難です。

あなたのコードは次のようになります:

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 に答える