2011-08-16 41 views
0

你好我試圖分析在iphone JSON字符串,到目前爲止我已經能夠得到JSON VALUE正確如何在iphone中解析JSON字符串Objective-C?

但在那之後我正在歌廳的錯誤:

-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62242e0 
2011-08-16 16:11:58.792 BleepBleep[4083:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62242e0' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x010a9be9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x011fe5c2 objc_exception_throw + 47 
    2 CoreFoundation      0x010ab6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x0101b366 ___forwarding___ + 966 
    4 CoreFoundation      0x0101af22 _CF_forwarding_prep_0 + 50 
    5 BleepBleep       0x0000733f -[Screen1 network:didFinishLoadingWithRequest:data:] + 79 
    6 BleepBleep       0x0000b7e4 -[WNetwork handleResponse] + 323 
    7 BleepBleep       0x0000b69b -[WNetwork connectionDidFinishLoading:] + 36 
    8 Foundation       0x00077172 -[NSURLConnection(NSURLConnectionReallyInternal) sendDidFinishLoading] + 108 
    9 Foundation       0x000770cb _NSURLConnectionDidFinishLoading + 133 
    10 CFNetwork       0x01674606 _ZN19URLConnectionClient23_clientDidFinishLoadingEPNS_26ClientConnectionEventQueueE + 220 
    11 CFNetwork       0x0173f821 _ZN19URLConnectionClient26ClientConnectionEventQueue33processAllEventsAndConsumePayloadEP20XConnectionEventInfoI12XClientEvent18XClientEventParamsEl + 293 
    12 CFNetwork       0x0166ae3c _ZN19URLConnectionClient13processEventsEv + 100 
    13 CFNetwork       0x0166acb7 _ZN17MultiplexerSource7performEv + 251 
    14 CoreFoundation      0x0108b01f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 
    15 CoreFoundation      0x00fe928b __CFRunLoopDoSources0 + 571 
    16 CoreFoundation      0x00fe8786 __CFRunLoopRun + 470 
    17 CoreFoundation      0x00fe8240 CFRunLoopRunSpecific + 208 
    18 CoreFoundation      0x00fe8161 CFRunLoopRunInMode + 97 
    19 GraphicsServices     0x019de268 GSEventRunModal + 217 
    20 GraphicsServices     0x019de32d GSEventRun + 115 
    21 UIKit        0x002e442e UIApplicationMain + 1160 
    22 BleepBleep       0x00002018 main + 102 
    23 BleepBleep       0x00001fa9 start + 53 
) 
terminate called after throwing an instance of 'NSException' 

繼承人本人代碼在網絡類使用didFinishLoadingWithRequest

-(void)network:(WNetwork*)network didFinishLoadingWithRequest:(NSInteger)pReq data:(NSMutableDictionary*)pData 
{ 
    [self removeLoader]; 

    switch (pReq) { 
     case JBJsonParser: 
     { 
      NSArray *parsedString = [pData objectForKey:@"placesname"]; 
      DLog(@"LIST %@",parsedString); 
     } 
    break;  
     default: 
      break; 
} 

} 

我使用DIS代碼:

{ 
    SBJSON *parser = [SBJSON new];  
    NSString *dataString = [[NSString alloc] initWithData:mRespData encoding:NSUTF8StringEncoding]; 


    NSMutableDictionary *newDic = [dataString JSONValue]; 

    if ([(id)mDelegate respondsToSelector:@selector(network:didFinishLoadingWithRequest:data:)]) { 
     [self.mDelegate network:self didFinishLoadingWithRequest:mReqType data:newDic]; 
    } 
    [newDic autorelease]; 

    [dataString release]; 
    [parser release]; 
} 
+0

dis是d字符串m試圖解析器jst萬一任何人想要d格式 http://beepbeepapp.com/showplaces.php – IphoneBites

回答

4

JSON點點:

這是一個JSON 陣列

["firstValue", "secondValue"] 

這是一個JSON 字典

{ 
"A key" : "A value", 
"Another key" : "Another value" 
} 

您的JSON告訴解析器根類型是數組。因此,jsonValue正在返回一個數組。 您正嘗試在該數組上調用objectForKey(NSDictionary方法)。這就是拋出異常的原因。

請發佈您的JSON,以便我們可以看到結構以及如何解析它。或者,嘗試記錄存儲jsonValue的對象。


UPDATE:

閱讀您的JSON之後,這是你應該如何對其進行解析:

NSString *jsonString; // set this to your json 
NSArray *places = [jsonString jsonValue]; 
// then iterate through the places, saving off the bits you need 
for (NSDictionary *place in places) { 
    NSString *placeName = [place objectForKey:@"placesname"]; // for example 
    NSLog(@"Name of place: %@", placeName); 
} 

什麼,你可能想要做的就是創建具有自定義類稱爲地方lat,long,placename等屬性,然後保存這些數組。

+0

請你能簡要介紹更多.. – IphoneBites

+0

解釋多一點 –

+0

我應該使用NSDictionary方法或數組,應該不是 NSArray * parsedString = [pData objectForKey:@「placesname」]; 或 NSDictionary * parsedString = [pData objectForKey:@「placesname」]; – IphoneBites

2

JSON語法代表數組和字典。當解析一個「未知」的JSON代碼段時,你不知道給定的「洋蔥層」是否是一個數組或字典,所以你必須檢查(在每個級別)看看你有什麼樣的對象。使用[myObject isKindOfClass:[NSArray class]][myObject isKindOfClass:[NSDictionary class]]

即使使用「已知」JSON源進行檢查也是不明智的,因爲網站可能會中斷或更改,並且最好提供一個很好的錯誤消息(並指責網站),而不是讓您的應用程序崩潰。

+0

不知道爲什麼這是downvoted。這是一個準確和有益的迴應。 – JaredH