如何从 NsDictionary 获取值

How to get the value from the NsDictionary

我试图从字典中获取值,但它给了我空值。实际上我已经解析了 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"。 但是我得到的响应是空值。请帮忙?是不是空格有问题?

您的字典中没有关键字 "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"];

您可以使用以下代码进入 GetServerStatusResult 中的 text

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

的值