0

我的XML響應:終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是:「 - [__ NSCFString objectForKey:]

{ 
    "S:Envelope" =  { 
     "S:Body" =   { 
      "ns2:getMatchListResponse" =    { 
       item =     (
             { 
         answerTime =       { 
          text = 30; 
         }; 
         challengerAppealsGranted =       { 
          text = "0 of 16"; 
         }; 
         challengerHandle =       { 
          text = manish; 
         }; 
         challengerMatchesPlayed =       { 
          text = 93; 
         }; 
         challengerPic =       { 
          text = "image.png"; 
         }; 
         challengerScore =       { 
          text = 0; 
         }; 
         challengerlosses =       { 
          text = 0; 
         }; 
         challengerwins =       { 
          text = 4; 
         }; 
         handle1 =       { 
          text = sahni; 
         }; 
         handle1AppealsGranted =       { 
          text = 5; 
         }; 
         handle1MatchesPlayed =       { 
          text = 84; 
         }; 
         handle1Response =       { 
          text = Pending; 
         }; 
         handle1Score =       { 
          text = 0; 
         }; 
         handle1losses =       { 
          text = 0; 
         }; 
         handle1wins =       { 
          text = 1; 
         }; 
         handle2MatchesPlayed =       { 
          text = 0; 
         }; 
         handle2Score =       { 
          text = 0; 
         }; 
         handle2losses =       { 
          text = 0; 
         }; 
         handle2wins =       { 
          text = 0; 
         }; 
         handle3MatchesPlayed =       { 
          text = 0; 
         }; 
         handle3Score =       { 
          text = 0; 
         }; 
         handle3losses =       { 
          text = 0; 
         }; 
         handle3wins =       { 
          text = 0; 
         }; 
         idMatch =       { 
          text = 750; 
         }; 
         isAppeal =       { 
          text = false; 
         }; 
         isFreeForm =       { 
          text = false; 
         }; 
         isMultichoiceQuestion =       { 
          text = false; 
         }; 
         isPublic =       { 
          text = false; 
         }; 
         matchName =       { 
          text = ewtwert; 
         }; 
         matchStartThresholdTime =       { 
          text = "4 days 7 hrs"; 
         }; 
         matchStatus =       { 
          text = wait; 
         }; 
         matchType =       { 
          text = Private; 
         }; 
         noOfPlayers =       { 
          text = 2; 
         }; 
         priorityInList =       { 
          text = 0; 
         }; 
         scoreToWin =       { 
          text = 5; 
         }; 
         timeDuration =       { 
          text = "5 days"; 
         }; 
         turnChangesIn =       { 
          text = 0; 
         }; 
        }, 
             { 
         answerTime =       { 
          text = 30; 
         }; 
         challengerAppealsGranted =       { 
          text = "0 of 16"; 
         }; 
         challengerHandle =       { 
          text = manish; 
         }; 
         challengerMatchesPlayed =       { 
          text = 93; 
         }; 
         challengerPic =       { 
          text = "image.png"; 
         }; 
         challengerScore =       { 
          text = 0; 
         }; 
         challengerlosses =       { 
          text = 0; 
         }; 
         challengerwins =       { 
          text = 4; 
         }; 
         handle1MatchesPlayed =       { 
          text = 0; 
         }; 
         handle1Score =       { 
          text = 0; 
         }; 
         handle1losses =       { 
          text = 0; 
         }; 
         handle1wins =       { 
          text = 0; 
         }; 
         handle2MatchesPlayed =       { 
          text = 0; 
         }; 
         handle2Score =       { 
          text = 0; 
         }; 
         handle2losses =       { 
          text = 0; 
         }; 
         handle2wins =       { 
          text = 0; 
         }; 
         handle3MatchesPlayed =       { 
          text = 0; 
         }; 
         handle3Score =       { 
          text = 0; 
         }; 
         handle3losses =       { 
          text = 0; 
         }; 
         handle3wins =       { 
          text = 0; 
         }; 
         idMatch =       { 
          text = 749; 
         }; 
         isAppeal =       { 
          text = false; 
         }; 
         isFreeForm =       { 
          text = false; 
         }; 
         isMultichoiceQuestion =       { 
          text = false; 
         }; 
         isPublic =       { 
          text = false; 
         }; 
         matchName =       { 
          text = gfhf; 
         }; 
         matchStartThresholdTime =       { 
          text = "4 days 6 hrs"; 
         }; 
         matchStatus =       { 
          text = wait; 
         }; 
         matchType =       { 
          text = Public; 
         }; 
         noOfPlayers =       { 
          text = 2; 
         }; 
         priorityInList =       { 
          text = 0; 
         }; 
         scoreToWin =       { 
          text = 5; 
         }; 
         timeDuration =       { 
          text = "5 days"; 
         }; 
         turnChangesIn =       { 
          text = 0; 
         }; 
        } 
       ); 
       "xmlns:ns2" = "http://services.tgs.com/"; 
      }; 
     }; 
     "xmlns:S" = "http://schemas.xmlsoap.org/soap/envelope/"; 
    }; 
} 

我需要關鍵的價值 「matchStartThresholdTime」。 我這樣做:

NSDictionary *dictResult = [XMLReader dictionaryForXMLString:responseString error:nil]; 
       NSDictionary *Enveloper = [dictResult objectForKey:@"S:Envelope"]; 
       NSDictionary *Body  = [Enveloper objectForKey:@"S:Body"]; 
       NSDictionary *profileDetails = [Body objectForKey:@"ns2:getMatchListResponse"]; 
       NSMutableArray *items = [profileDetails objectForKey:@"item"]; 

      // NSLog(@"items===>%@",items); 

       NSDictionary *temp1; 

       for(temp1 in items) 
       { 

        thresholdTime = [NSString stringWithFormat:@"%@",[[temp1 objectForKey:@"matchStartThresholdTime"]objectForKey:@"text"]];...thresholdTime is NSString object.......and this line causes exception 

        //NSLog(@"time is===>%@",[[temp1 objectForKey:@"matchStartThresholdTime"]objectForKey:@"text"]); 
       } 

這裏有什麼問題?

回答

0

Temp1包含NSString對象,而不是您期望的NSDictionary

在處理JSON時,明智的做法是(雙)檢查接收到的對象是否真的是您期望的類型。始終使用

if ([temp1 isKindOfClass:[NSDictionary class]]) ... 

左右。

+0

它是XML,而不是JSON - 生成的數據結構的NSLog類似於JSON,但是如果仔細觀察則會更復雜。但是你的評論認爲,仔細檢查對象類型通常是明智的,尤其是在調試新的場景時。 – 2013-07-09 14:51:58

+0

你說得對。我被數據結構的類似json的輸出分散了注意力。然而,無論如何,明智的雙重檢查是收到預期的結果。 – 2013-07-09 15:04:28

相關問題