2016-08-23 61 views
-3

我在这行代码,iOS-初始化自定义类使用JSON返回nil

Country *country = [[Country alloc] initWithNSDictionary:jsonObject]; 

我得到一个元素的JSONObject但为什么国家在零?

我在项目中有国家级文件,我已经通过它做了一个对象。

JSON响应:

{"geonames": [{ 
    "continent": "AS", 
    "capital": "Nuova Delhi", 
    "languages": "en-IN,hi,bn,te,mr,ta,ur,gu,kn,ml,or,pa,as,bh,sat,ks,ne,sd,kok,doi,mni,sit,sa,fr,lus,inc", 
    "geonameId": 1269750, 
    "south": 6.747139, 
    "isoAlpha3": "IND", 
    "north": 35.504223, 
    "fipsCode": "IN", 
    "population": "1173108018", 
    "east": 97.403305, 
    "isoNumeric": "356", 
    "areaInSqKm": "3287590.0", 
    "countryCode": "IN", 
    "west": 68.186691, 
    "countryName": "India", 
    "continentName": "Asia", 
    "currencyCode": "INR" 
}]} 

initWithNSDictionary方法

- (instancetype) initWithNSDictionary:(NSDictionary*)countryInfo_ 
{ 
    self = [super init]; 
    if(self) { 
     NSLog(@"Country Info = %@",countryInfo_); 
     self.code = [countryInfo_ valueForKey:@"countryCode"]; 
     self.name = [countryInfo_ valueForKey:@"countryName"]; 
     self.continent = [countryInfo_ valueForKey:@"continentName"]; 
     self.region = [countryInfo_ valueForKey:@"region"]; 
     self.currencyCode = [countryInfo_ valueForKey:@"currencyCode"]; 
     self.population = [countryInfo_ valueForKey:@"population"]; 

    } 
    NSLog(@"code %@", code); 

    return self; 
} 
+0

在问题中增加'initWithNSDictionary'方法。第二件事改变你的问题的标题。它应该与您的查询或问题有关! – Lion

+0

更改标题。你有解决方案吗? @KetanParmar – KAR

+2

你的'initWithNSDictionary' mehod做什么?据说,任何人都可以给答案! – Lion

回答

0

改变的代码行到

Country *country = [[Country alloc] initWithNSDictionary:(NSDictionary *)jsonObject]; 

和访问变量从国家使用

[countryCode setText:[country.code description]]; 

此使用说明解决我的问题。

0

使用此上

Country *country = [[Country alloc] initWithNSDictionary:[jsonObject objectForKey:@"your key"]]; 

//[[NSDictionary alloc] initWithObjectsAndKeys: 
        //jsonObject ?: [NSNull null], @"jsonObject",nil] 
+0

这是我的JSONObject - 的JSONObject( { areaInSqKm = “3287590.0”; 资本= “Nuova的德里”; 大陆= AS; continentName =亚洲; COUNTRYCODE = IN; 国家名称=印度; CURRENCYCODE = INR; 东= “97.403305”; fipsCode = IN; geonameId = 1269750; isoAlpha3 = IND; isoNumeric = 356; 北= “35.504223” ; 人口= 1173108018; south =“6.747139”; west =“68.186691”; } )@SMi – KAR

+0

@KahanR请在您的问题中添加api的回复而不是评论。 –

+0

如果要使用jsonObject的值初始化Country对象的属性,则必须通过键将每个值插入到这些属性中 – Sofeda

1

由于您没有提供足够的问题信息,我写的答案假设基础:

在您的CountryinitWithNSDictionary方法应该定义类似这样的东西。

-(instancetype)initWithNSDictionary : (NSDictionary*)dictionary{ 

self = [super init]; 

if (self) { 

    self.firstName = [dictionary objectForKey:@"firstName"]; //This is just example because you haven't add sufficient information in question 
    self.lastName = [dictionary objectForKey:@"lastName"]; 
} 

return self; 
} 

然后,你可以为你在你的问题已经创建创建实例或Country对象,

Country *country = [[Country alloc] initWithNSDictionary:jsonObject]; 

确保JSON对象有你在initWithNSDictionary方法中使用的关键和价值。我刚刚举了名字和姓氏的例子。您应该根据您的json对象管理initWithNSDictionary中的数据。

+0

他现在已经添加了'initWithDictionary'方法,请查看 – NSNoob

+1

为什么使用'valueForKey:'? – Droppy

+1

他的缺点是他没有从他的JSON(dict-> arr-> dict模型)中提取最内部的字典,这就是为什么他的值提取返回nil。告诉他如何得到最内在的字典,你的答案就会完成。当然,Droppy说什么 – NSNoob

1

你的结构是:

-Dictionary 
--Array 
---Dictionary 

这意味着你有一个包含字典数组的顶部字典。

你的错误是,你试图用顶级字典初始化你的对象,而顶级字典没有提供你提到的值的键,所以它返回nil,你的对象依次有nil值。

简单地说,要传递其仅具有下列键值对的字典:

Key: Geonames 
Value: An Array 

然后在您的init方法假定你是它包含了其他键的时候,其实那些其他键存在仅在数组中包含的字典中。这是下面的字典:

{ 
    "continent": "AS", 
    "capital": "Nuova Delhi", 
    "languages": "en-IN,hi,bn,te,mr,ta,ur,gu,kn,ml,or,pa,as,bh,sat,ks,ne,sd,kok,doi,mni,sit,sa,fr,lus,inc", 
    "geonameId": 1269750, 
    "south": 6.747139, 
    "isoAlpha3": "IND", 
    "north": 35.504223, 
    "fipsCode": "IN", 
    "population": "1173108018", 
    "east": 97.403305, 
    "isoNumeric": "356", 
    "areaInSqKm": "3287590.0", 
    "countryCode": "IN", 
    "west": 68.186691, 
    "countryName": "India", 
    "continentName": "Asia", 
    "currencyCode": "INR" 
} 

所以你需要从数组中获得上述字典,并用它来初始化你的对象。

要修复它,你需要修改你的initWithDictionary方法(或者理想情况下,你调用initWithdictionary方法的方式,但现在让我们忘记它)。

- (instancetype) initWithNSDictionary:(NSDictionary*)countryInfo_ 
{ 
    self = [super init]; 
    if(self) { 
     NSLog(@"Country Info = %@",countryInfo_); 
     NSArray *internalArray = countryInfo_[@"geonames"]; //Now you got your array of dictionaries. 
     if([internalArray count]>0){ 
      NSDictionary *internalDictionary = internalArray[0]; //Assuming there will always be only one dictionary in that array but if there are more, thats your design problem. You got your internal dictionary now 
      self.code = internalDictionary[@"countryCode"]; 
      self.name = internalDictionary[@"countryName"]; 
      self.continent = internalDictionary[@"continentName"]; 
      self.region = internalDictionary[@"region"]; 
      self.currencyCode = internalDictionary[@"currencyCode"]; 
      self.population = internalDictionary[@"population"]; 
     } 


    } 
    NSLog(@"code %@", code); 

    return self; 
} 

注:这是假设将永远存在于你的字典数组一个字典或至少只是你想使用的第一个。这意味着你有一些信息隐瞒了我们,或者在你的设计上有一个很大的设计缺陷。

+0

为什么要分配'internalDictionary'而不是引用数组中现有的? – Droppy

+0

@Droppy我喜欢这种方式。它可以简单地通过'[internalArray objectAtIndex:0]'来访问。查看编辑历史记录,我刚刚写道,但并不真的喜欢它。有点让我觉得不安全 – NSNoob

+0

但是,是的,而不是偏执,它真的浪费一个实例,所以会恢复到 – NSNoob