1

我的 XML 响应:

{
    "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"]);
               }

这里有什么问题?

4

1 回答 1

0

Temp1 包含您期望的NSStringObject 而不是, 。NSDictionary

在处理 JSON 时,明智的做法是(双重)检查接收到的对象是否真的是您期望的类型。始终使用

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

或者。

于 2013-05-03T08:43:56.210 回答