如何从NsDictionary获取值

时间:2015-01-07 10:24:36

标签: ios nsdictionary

我试图从字典中获取值但它给我null。实际上我已经解析了XML然后将其转换为字典以下是转换后的字典:(我在日志中得到它)

dictionary: {
    "soap:Envelope" =     {
        "soap:Body" =         {
            GetServerStatusResponse =             {
                GetServerStatusResult =                 {
                    text = "<NewDataSet>\n  <Table1>\n    <ServerStatus>Standby</ServerStatus>\n  </Table1>\n</NewDataSet>";
                };
                xmlns = "http://tempuri.org/";
            };
        };
        "xmlns:soap" = "http://schemas.xmlsoap.org/soap/envelope/";
        "xmlns:xsd" = "http://www.w3.org/2001/XMLSchema";
        "xmlns:xsi" = "http://www.w3.org/2001/XMLSchema-instance";
    };
}

现在我想从 ServerStatus 标记中获取值。在这种情况下,就是“StandBy”。 但我得到空值作为回应。请帮忙?是否有空格问题?

2 个答案:

答案 0 :(得分:1)

词典中没有键"ServerStatus"。 唯一的关键是"soap:Envelope"

"soap:Body""xmlns:soap""xmlns:xsd""xmlns:xsi"是与密钥"soap:Envelope"对应的词典的键。

您无法轻松访问"ServerStatus",您可以做的是加入text,然后解析与检索"ServerStatus"相关联的值

你应该能够以text的方式获得价值:

[[[[[dictionary objectForKey:@"soap:Envelope"] objectForKey:@"soap:Body"] objectForKey:@"GetServerStatusResponse"] objectForKey:@"GetServerStatusResult"] objectForKey:@"text"];

答案 1 :(得分:0)

您可以使用以下代码访问text中的GetServerStatusResult

NSDictionary *soapEnvelope   = [dictionary valueForKey:@"soap:Envelope"];
NSDictionary *soapBody       = [soapEnvelope valueForKey:@"soap:Body"];
NSDictionary *statusResponse = [soapBody valueForKey:@"GetServerStatusResponse"];
NSDictionary *statusResult   = [statusResponse valueForKey:@"GetServerStatusResult"];

NSString *xmlString = [statusResult valueForKey:@"text"];
NSDictionary *textDictionary = [XMLDictionary dictionaryWithXMLString:xmlString];

现在text键中的xml转换为NSDictionary,您可以轻松获得ServerStatus的值