Org.Json - JSONException 处理

如果 JSON 无效,org.json 的实用程序类将抛出 JSONException。以下示例显示如何处理 JSONException。

示例

import org.json.JSONException;
import org.json.XML;

public class JSONDemo {
   public static void main(String[] args) {
      try {
         //XML标签名称不能有空格。
         String xmlText = "<Other Details>null</Other Details>";
         System.out.println(xmlText);

         //将 XML 转换为 JSONObject
         System.out.println(XML.toJSONObject(xmlText));
      } 
      catch(JSONException e){   
         System.out.println(e.getMessage());
      }
   }
}

输出

<Other Details>null</Other Details>
Misshaped close tag at 34 [character 35 line 1]