即使密钥路径存在,NSDictionary valueForKeyPath也会返回nill

时间:2017-07-28 10:37:03

标签: ios objective-c json nsdictionary

这是我的应用程序中的两行。第一个正常工作,我得到的价值" LE-05330"在标签上但是对于第二个我没有。我在第二行做错了什么?

//Working properly
self.lblSerialNo.text = [self.PartInfo valueForKeyPath:@"veri.parca_bilgisi.referans_no"];

//valueForKeyPath returns nil so label is empty.
self.lblCertNo.text = [self.PartInfo valueForKeyPath:@"veri.sertifika.sertifika_no"];

self.PartInfo对象上的JSON数据如下;

{
    "durum": true,
    "hata": "",
    "veri": {
        "parca_bilgisi": {
            "imaj": "http://www.mywebsite.com:9898/mobapp/images/arac_hasar/arac-onsolkapi.jpg",
            "parca_tanimi": "ÖN SOL KAPI",
            "marka": "MYBRAND",
            "marka_kodu": "70",
            "model": "MYMODEL",
            "model_kodu": "?",
            "yil": "2005",
            "uretici": "MYManufacturer.",
            "referans_no": "LE-05330",
            "oem_no": "999999",
            "durum": "3",
            "durum_aciklama": "Transferred_to_Supplier"
        },
        "onarim_servis_bilgileri": {
            "servis_adi": "?",
            "iletisim_no": "?",
            "islem_tarihi": "?",
            "hasar_dosya_no": "?"
        },
        "sigorta_bilgileri": {
            "firma_adi": "?",
            "iletisim_no": "?",
            "ilgili_kisi": "?"
        },
        "eksper_bilgileri": {
            "onaylama_tarihi": "?"
        },
        "uretici_bilgileri": {
            "firma": "My Firm AS.",
            "imaj": "harita.jpg",
            "adres": "Carrer de Jacint Verdaguer, 58, 08970",
            "eposta": "myr@email.es",
            "tel": "+36 56565 345 317",
            "web": "www.styaasira.es",
            "kayit_tarihi": "7.7.2017 13:27:44",
            "durum": "3"
        },
        "sertifika": {
            "sertifikasyon_kurulusu": "87",
            "sertifika_no": "87",
            "gecerlilik_tarihi": "87"
        }
    }
} 

我检索NSDictionary(dict)的代码

NSURLSessionDataTask* task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {       
        if (error == nil) {
            // Success
            NSMutableDictionary* dict = [NSJSONSerialization
                                           JSONObjectWithData:data
                                           options:kNilOptions
                                           error:&error];

我取消了服务调用,并尝试了与字符串相同的JSON,但仍然没有机会......

NSString *json = ...

    NSError *error;
    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:data
                                                         options:NSJSONReadingMutableLeaves
                                                           error:&error];

    self.lblIssuer.text = [dict valueForKeyPath:@"veri.sertifika.sertifikasyon_kurulusu​"];

在几次不成功的逻辑尝试之后,我决定专注于数据并尝试修改(simplfy)JSON对象并尝试查看它是否适用于不同的键名。所以我首先从" sertifika"更改了节点的名称。到"测试"并且3个子节点名称为" a"," b"和" c"。通过这种状态,我可以使用valueForKeyPath获取值。然后,我将节点的名称更改回原始,它也有效!然后我注释了从常量字符串到服务响应读取JSON,...等待它......它也起作用!!! ....

任何人都能说出正在发生的事情吗? NSDictionary是否有一种疯狂的散列机制?无论如何,更改失败然后回到原始的节点名称已经解决了我的问题......

0 个答案:

没有答案