org.json - JSONException Handling


Advertisements

Utility classes of org.json throws JSONException in case of invalid JSON. Following example shows how to handle 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

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

Useful Video Courses


Video

Reproduction in Organisms

18 Lectures 1.5 hours

Dr. Saatya Prasad

Video

Computer Organization

107 Lectures 13.5 hours

Arnab Chakraborty

Video

Basic Principles of Organic Chemistry

75 Lectures 5 hours

Revathi Ramachandran

Video

Work Area Organization for Increased Office Productivity

14 Lectures 44 mins

Zach Miller

Video

Get Organized

12 Lectures 54 mins

Prof. Paul Cline, Ed.D

Video

Organic Chemistry: How to Build People

54 Lectures 4 hours

Gilad James, PhD

Advertisements