Org Json 简明教程

Org.Json - JSONException Handling

org.json 的实用程序类在 JSON 无效的情况下会引发 JSONException。以下示例显示如何处理 JSONException。

Example

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

public class JSONDemo {
   public static void main(String[] args) {
      try {
         //XML tag name should not have space.
         String xmlText = "<Other Details>null</Other Details>";
         System.out.println(xmlText);

         //Convert an XML to JSONObject
         System.out.println(XML.toJSONObject(xmlText));
      }
      catch(JSONException e){
         System.out.println(e.getMessage());
      }
   }
}

Output

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