English 中文(简体)
JSON.simple - Exception Handling
  • 时间:2024-12-22

JSON.simple - Exception Handpng


Previous Page Next Page  

JSONParser.parse() throws ParseException in case of invapd JSON. Following example shows how to handle ParseException.

Example

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

class JsonDemo {
   pubpc static void main(String[] args) {
      JSONParser parser = new JSONParser();
      String text = "[[null, 123.45, "a	b 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);
      }
   }
}

Output

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