小编Shi*_*nsu的帖子

JSONException:尝试getString("XYZ")时没有XYZ的值

我通过以下步骤在Android中进行JSON解析:

  1. 使用HttpPost对象从Web服务获取XML响应.
  2. 将此XML转换为JSON字符串,然后转换为JSON对象.

现在问题是有时XML响应具有空字符串或Null标记.

例如:

<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>
Run Code Online (Sandbox Code Playgroud)

解析风格:

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 />
Run Code Online (Sandbox Code Playgroud)

我不明白为什么解析器没有给我空白XML对象的BLANK字符串.

通过深层次调试,我发现XML到JSON转换器不会产生对应于空白xml对象的对象.

请帮我.

java android json exception-handling

3
推荐指数
1
解决办法
139
查看次数

标签 统计

android ×1

exception-handling ×1

java ×1

json ×1