【发布时间】:2014-05-11 07:33:30
【问题描述】:
我正在通过以下步骤在 Android 中进行 JSON 解析:
- 使用 HttpPost 对象从 Web 服务获取 XML 响应。
- 将此 XML 转换为 JSON 字符串,然后转换为 JSON 对象。
现在的问题是有时 XML 响应有空字符串或空标签。
例如:
<data>
<name>Martin Clark</name>
<city>London</city>
<country>XYZ</country> or <country /> <!-- Sometimes it will blank string like this if country is not available -->
<age>27</age>
</data>
解析风格:
jsonObject.getString("country"); // It is working perfect when xml is this : <country>XYZ<country/>
jsonObject.getString("country"); // It is giving Exception key is not found when xml is this : <country />
我不明白为什么解析器没有为我提供空白 XML 对象的 BLANK 字符串。
通过深度调试,我发现 XML 到 JSON 的转换器不会产生对应于空白 xml 对象的对象。
请帮帮我。
【问题讨论】:
-
为什么将xml转换成json可以自己解析xml
标签: java android json exception-handling