📜  org.json-JSONException处理

📅  最后修改于: 2020-11-12 05:10:06             🧑  作者: Mango


如果JSON无效,则org.json实用程序类将引发JSONException。下面的示例演示如何处理JSONException。

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 = "null";
         System.out.println(xmlText);

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

输出

position: 24
Unexpected token RIGHT BRACE(}) at position 24.