JSONni ajratish - SBJsonParser bilan muammo

SBJsonParserdan foydalanishda ba'zi JSONni tahlil qilishda hozirda g'alati muammo yuzaga keladi.

Endi, men ajraladigan JSON quyidagilar.

[
   {
      "Company":{
         "id":"1",
         "company_name":null
      },
      "relations":{
         "store":[
            {
               "id":"1",
               "restaurant_name":"Dubai",
               "brief_description":null
            },
            {
               "id":"2",
               "restaurant_name":"Test2",
               "brief_description":null
            }
         ]
      }
   }
]

Men osonlik bilan NSDictionary yaratib, uni Kompaniya tuguniga (?) To'g'ri ma'lumot bilan to'ldirishim mumkin.

Lekin mening masala munosabatlar va do'kon tugunlariga kelganda kelib chiqadi.

 NSDictionary *relations = [object valueForKey:@"relations"];
 NSArray *multipleStores = [relations valueForKey:@"store"];
 NSLog(@"relations: %@", relations);

 for (NSDictionary *singleStore in multipleStores){

        NSLog(@"Single Store: %@", singleStore);

        [company grabStoreInformation:singleStore];

 }

NSLogning yuqorida qaytib kelishi bu.

 relations: (
    {
    store =         (
                    {
            "brief_description" = "";
            id = 1;
            "restaurant_name" = Dubai;
        },
                    {
            "brief_description" = "";
            id = 2;
            "restaurant_name" = Test2;
        }
    );
 }
)

Endi agar bu NSLogda sodir bo'layotgan bo'lsa, bu yaxshi bo'lar edi. SingleStore, aslida, birma-bir do'kon düğümlerini emas, balki, do'kon düğümlerinin har ikkalasini qo'shib kabi ko'rinadi.

Single Store: (
    {
    "brief_description" = "";
    id = 1;
    "restaurant_name" = Dubai;
    },
    {
    "brief_description" = "";
    id = 2;
    "restaurant_name" = Test2;
    }
)

Muammo shundaki, har bir do'kon tuguniga NSMutableArray qo'shilishi kerak. Shunday qilib, NSDictionary keyinchalik NSMutableArray-ga qo'shiladi va undan keyin boshqa joyda mavjud bo'ladi (UITableView ma'lumotlar manbai uchun).

Har qanday yordam do'kon nodlarini alohida olishda katta baxtdir.

EDIT As asked for, entire code for parsing:

        SBJsonParser *parser = [[SBJsonParser alloc] init];

       //parse the JSON string into an object - assuming [response asString] is a NSString of JSON data
        NSDictionary *object = [parser objectWithString:[response asString] error:nil];

        [parser release];

        NSDictionary *companyDetails = [object valueForKey:@"Company"];

        MACompany *company = [MACompany sharedMACompany];
        [company initWithDetails:companyDetails];

        NSDictionary *relations = [object valueForKey:@"relations"];
        NSArray *multipleStores = [relations valueForKey:@"store"];

        NSLog(@"relations: %@", relations);

        for (NSDictionary *singleStore in multipleStores){

            NSLog(@"Single Store: %@", singleStore);

            [company grabStoreInformation:singleStore];

        }

Ko'rib turganingizdek, JSON elementlarini nusxalash uchun bitta singleton klassiga tayanaman. Bu, menimcha, bir do'kon lug'atini qanday ajratish kerakligini tushunishga harakat qilaman.

0
Yopiq mavzu, ammo OS5-ni maqsad qilib olgan bo'lsangiz, ichki JSON parseridan foydalanishingiz mumkin
qo'shib qo'ydi muallif Devraj, manba
@SebastienPeek fair enough :)
qo'shib qo'ydi muallif Devraj, manba
Biz iOS4-ni ham qo'llab-quvvatlaymiz. Menimcha, har ikkala iOS uchun ham xuddi shu jarayondan foydalaning.
qo'shib qo'ydi muallif Sebastien Peek, manba

1 javoblar

Birinchidan, sizning yuqori darajadagi elementingiz ob'ekt emas, balki qatordir. Shuning uchun:

NSDictionary *object = [parser objectWithString:[response asString] error:nil];

bo'lishi kerak:

NSArray *object = [parser objectWithString:[response asString] error:nil];

va o'zgarmaydigan nom sifatida ob'ekt o'rniga companies foydalanmoqchiman.

Ikkinchidan, -valueForKey: dan foydalanishda ehtiyot bo'lishingiz kerak. Agar qatorga yuborilsa, key argümaniga mos keladigan qiymatlarni o'z ichiga olgan boshqa qator funksiyasini qaytaradi. Agar siz kalit-qiymat kodlashni bilmagan bo'lsangiz, NSDictionary da kanonik usulni foydalaning: -objectForKey: .

Agar -valueForKey: o'rniga -objectForKey: dan foydalangan bo'lsangiz, dasturni ishga tushirganingizda ushbu tuzilish xatoligini sezgan bo'lasiz.

Ushbu ikki fikrni yodda tutgan holda, JSON ma'lumotingizga quyidagi tarzda o'tishingiz mumkin:

NSArray *companies = [parser objectWithString:[response asString] error:nil];
for (NSDictionary *company in companies) {
    NSDictionary *companyDetails = [company objectForKey:@"Company"];
    NSDictionary *relations = [company objectForKey:@"relations"];
    NSArray *stores = [relations objectForKey:@"store"];

    for (NSDictionary *store in stores) {
        NSString *restaurantName = [store objectForKey:@"restaurant_name"];
        …
    }

Stack Overflow-da bu boshqa savolni o'qishni tavsiya etaman: Difference valueforKey objectForKey va Key-Value Coding Programming Guide < kodi>.

1
qo'shib qo'ydi
@Sebastien Albatta. Boshqa bir variant esa do'konlar majmuasini o'zgarishi mumkin nusxasini yaratish, masalan, NSMutableArray * storesArray = [mutableCopy ni saqlaydi] .
qo'shib qo'ydi muallif user557219, manba
Muvaffaqiyatli ishlaydi. Mening yagona muammoim, NSDictionary * do'konida NSMutableArray-ga qo'shishni xohlayman, shuning uchun uni keyinroq chiqarib olishim mumkin, faqatgina [NSMutableArray addObject: store] qilsa bo'ladi?
qo'shib qo'ydi muallif Sebastien Peek, manba
rahmat, juda foydali!
qo'shib qo'ydi muallif Sebastien Peek, manba