我的代码中有这一行,
Country *country = [[Country alloc] initWithNSDictionary:jsonObject];
我在 jsonObject 中获得了一个元素,但为什么 country 在nil中?
我在项目中有Country类文件,我已经通过它创建了一个对象。
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 :(得分:1)
由于您还没有提供足够的信息,我正在根据假设写答案:
在Country
班initWithNSDictionary
方法中,应该定义类似的内容。
-(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
方法中使用的键和值。我刚刚给出了firstname和lastname的例子。您应该根据您的json对象管理initWithNSDictionary
中的数据。
答案 1 :(得分:1)
您的结构是:
-Dictionary
--Array
---Dictionary
这意味着您有一个包含字典数组的顶级字典。
您的错误是,您尝试使用顶级字典初始化您的对象,该字典没有针对您提及的值的键,因此它返回nil并且您的对象依次具有nil值。
简而言之,您传递的字典只包含以下键和值对:
Key: Geonames
Value: An Array
然后在你的init方法中你假设它包含其他键,而实际上那些其他键只存在于数组中包含的字典中。以下是dict:
{
"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"
}
所以你需要从数组中获取上面提到的Dictionary并使用它来初始化你的对象。
要修复它,你需要修改你的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;
}
注意:这假设您的字典数组中总会有一个字典,或者至少只是您想要使用的第一个字典。这意味着您有一些隐瞒我们的信息或者您最终的一个非常大的设计缺陷。
答案 2 :(得分:0)
使用此
Country *country = [[Country alloc] initWithNSDictionary:[jsonObject objectForKey:@"your key"]];
//[[NSDictionary alloc] initWithObjectsAndKeys:
//jsonObject ?: [NSNull null], @"jsonObject",nil]
答案 3 :(得分:0)
将代码行更改为
Country *country = [[Country alloc] initWithNSDictionary:(NSDictionary *)jsonObject];
和来自国家/地区的访问变量
[countryCode setText:[country.code description]];
这通过使用说明解决了我的问题。