2012-06-21 46 views
1
//initialize the people data list 
- (void)initializePeopleListFromJson:(CLLocationCoordinate2D)coordinate auraId:(NSString *)auraId 
{ 

//initialize the NSMutableArray list 
self.peopleList = [[NSMutableArray alloc] init]; 

//retrieve the coordinate of myself 
//NSString *myCoordinate = [NSString stringWithFormat:@"%f,%f", coordinate.latitude, coordinate.longitude]; 
NSString *myCoordinate = @"31.2,121.6"; 

NSString *url = [NSString stringWithFormat:@"%@%@%@%@", @"http://services.portaura.mobi/AuraMesh/auraFinder/findPeoples?ll=", myCoordinate, @"&source=tencent,netease,kaixin&myAuraId=", auraId]; 

NSLog(@"*********** %@", url); 

//retrieve the people list from web service 
NSDictionary* result = [NSDictionary dictionaryWithContentsOfJSONURLString:url]; 
NSArray *peopleListFromJson = [result objectForKey:@"data"]; 

// NSLog(@「peopleList:%@」,peopleListFromJson);NSCFString objectForKey無法識別的選擇器發送到實例

People *people; 
UserProfile *userProfile; 
NSDictionary *geoFromJson; 
NSDictionary *profileFromJson; 



for (NSDictionary *peopleFromJson in peopleListFromJson) 
{ 

    people = [[People alloc] init]; 
    userProfile = [[UserProfile alloc] init]; 

    people.foreignId = [peopleFromJson objectForKey:@"foreignId"]; 
    people.id1 = [peopleFromJson objectForKey:@"id"]; 
    people.isFavorited = [peopleFromJson objectForKey:@"isFavorited"]; 
    people.lastActiveTime = [peopleFromJson objectForKey:@"lastActiveTime"]; 
    people.lastActivity = [peopleFromJson objectForKey:@"lastActivity"]; 
    people.lastPlace = [peopleFromJson objectForKey:@"lastPlace"]; 
    people.source = [peopleFromJson objectForKey:@"source"]; 

    NSLog(@"AAAAAAAA %@", [peopleFromJson objectForKey:@"foreignId"]); 


    //process geo 
    geoFromJson = [[NSDictionary alloc] init]; 
    geoFromJson = [peopleFromJson objectForKey:@"geo"]; 

    CLLocationCoordinate2D coordinate; 
    coordinate.latitude = [[geoFromJson objectForKey:@"lat"] floatValue]; 
    coordinate.longitude = [[geoFromJson objectForKey:@"lng"] floatValue]; 

    people.geo = coordinate; 
    people.distance = [geoFromJson objectForKey:@"distance"]; 


    //process profile 
    profileFromJson = [[NSDictionary alloc] init]; 
    profileFromJson = [peopleFromJson objectForKey:@"profile"]; 

    people.avatar = [profileFromJson objectForKey:@"avatar"]; 
    people.gender = [profileFromJson objectForKey:@"gender"]; 
    people.location = [profileFromJson objectForKey:@"location"]; 
    people.screenName = [profileFromJson objectForKey:@"screenName"]; 
    people.signature = [profileFromJson objectForKey:@"sigunature"]; 

    //people.userProfile = userProfile; 
    [self addPeople:people]; 

} 

}

it give me the [__NSCFString objectForKey:]: unrecognized selector sent to instance 0x1808d0,can you give some advice 

JSON像:

{ 「狀態」:0 「數據」:{ 「清單」:[{ 「foreignId」: 「8827857641648129226」,「地理「:{」 距離 「:1359,」 LAT 「:31.20926508184017,」 LNG 「:121.59068046014856},」 ID 「:」 netease_8827857641648129226" , 「isFavorited」:假 「lastActiveTime」:「2012-05-19T20:26:47Z 「,」lastActivity「:」http://126.fm/kFEKl「,」lastPlace「:」「,」profile「 HTTP% 3A%2F%2Fmimg.126.net%2FP%2Fbutter%2F1008031648%2Fimg%2Fface_big.gif」, 「性別」:0, 「位置」: 「」, 「屏幕名」: 「4671784460」, 「sigunature」: 「」 ,「tags」:[]},「source」:「netease」} ......

+2

最有可能的'profileFromJson = [peopleFromJson objectForKey:@ 「檔案」];'返回的NSString,而不是一個的NSDictionary ....請問在JSON真的看? –

+0

我是一個新的iOS開發人員,我正在修改其他人的代碼。謝謝 – pengwang

+0

我認爲你總是不對,因爲依賴於JSON,有時候正確的是NSDictionary。有時是NSString。 – pengwang

回答

8

檢查你的變量類peopleFromJson它在你調用objectForKey時可能不是字典。

把這個語句作爲第一行烏爾for
NSLog(@"%@",NSStringFromClass([peopleFromJson class]));

+0

你是對的,peopleFromJson不是一個NSDictionary,你可以給其他搜索鍵來幫助我解決問題。和JSON的鏈接還可以謝謝你 – pengwang

0

我已經解決了這個問題,我不能認爲這個問題是容易的,也許有些人只知道原因是該對象的NSString不NSDir,但真正的問題是取決於JSON「風格」,有時我的代碼是正確的,但對於其他JSON是不正確的,例如我的問題JSON。所以解決的問題是:

if (![peopleListFromJson isKindOfClass:[NSArray class]] && peopleListFromJson!=nil) { 
    peopleListFromJson =[NSArray arrayWithObject:peopleListFromJson]; 
} 

because i used 
NSDictionary* result = [NSDictionary dictionaryWithContentsOfJSONURLString:url]; 
NSArray *peopleListFromJson = [result valueForKeyPath:@"data.list"]; 
so peopleListFromJson!=nil is impotent 
相關問題