我试图从字典中获取值但它给我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”。 但我得到空值作为回应。请帮忙?是否有空格问题?
答案 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
的值