Json-simple-exception-handling

提供:Dev Guides
移動先:案内検索

JSON.simple-例外処理

JSONParser.parse()は、無効なJSONの場合にParseExceptionをスローします。 次の例は、ParseExceptionを処理する方法を示しています。

import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

class JsonDemo {
   public static void main(String[] args) {
      JSONParser parser = new JSONParser();
      String text = "[[null, 123.45, \"a\tb c\"]}, true";

      try{
         Object obj = parser.parse(text);
         System.out.println(obj);
      }catch(ParseException pe) {
         System.out.println("position: " + pe.getPosition());
         System.out.println(pe);
      }
   }
}

出力

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