Org-json-exception-handling

提供:Dev Guides
2020年6月23日 (火) 07:47時点におけるMaintenance script (トーク | 投稿記録)による版 (Imported from text file)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先:案内検索

org.json-JSONException処理

org.jsonのユーティリティクラスは、無効な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 = "<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());
      }
   }
}

出力

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