Home > Software design >  Why am I getting this error ?? java.lang.ClasscastException
Why am I getting this error ?? java.lang.ClasscastException

Time:03-18

I am unable to resolve the issue.. please help:

Erorr:

java.lang.ClassCastException: class org.json.simple.JSONArray cannot be cast to class org.json.simple.JSONObject (org.json.simple.JSONArray and org.json.simple.JSONObject are in module [email protected] of loader 'app')
    at lamda_pi/lamda_pi.letsRead.main(letsRead.java:77)

Sample json looks like below:

[
{
"name": "Afghanistan",
"dial_code": " 93",
"code": "AF"
},
{
"name": "Aland Islands",
"dial_code": " 358",
"code": "AX"
}]
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;


 class letsRead 
 {
    public static void main(String [] args)
    {
        String inline = "";
    
        try
        {
            URL url = new URL("https://gist.githubusercontent.com/anubhavshrimal/75f6183458db8c453306f93521e93d37/raw/f77e7598a8503f1f70528ae1cbf9f66755698a16/CountryCodes.json");
                    
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    
            conn.setRequestMethod("GET");
            
            conn.connect();
            
            int responsecode = conn.getResponseCode();
            System.out.println("Response code is: "  responsecode);
            
            if(responsecode != 200)
                throw new RuntimeException("HttpResponseCode: "  responsecode);
            else
            {
                Scanner sc = new Scanner(url.openStream());
                while(sc.hasNext())
                {
                    inline=inline   sc.nextLine();
                }
                System.out.println("\nJSON Response in String format"); 
                System.out.println(inline);
                sc.close();
            }
           
            JSONParser parse = new JSONParser();
            
            JSONObject jobj = (JSONObject)parse.parse(inline);
            
            JSONArray jsonarr_1 = (JSONArray) jobj.get("name");
        
            for(int i=0;i<jsonarr_1.size();i  )
            {
                
                JSONObject jsonobj_1 = (JSONObject)jsonarr_1.get(i);
                System.out.println(jsonobj_1.get("name"));
                
                
            }
                    conn.disconnect();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
 }

CodePudding user response:

The top level object in your JSON file is an array so:

class letsRead 
 {
    public static void main(String [] args)
    {
        String inline = "";
    
        try
        {
            URL url = new URL("https://gist.githubusercontent.com/anubhavshrimal/75f6183458db8c453306f93521e93d37/raw/f77e7598a8503f1f70528ae1cbf9f66755698a16/CountryCodes.json");
                    
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    
            conn.setRequestMethod("GET");
            
            conn.connect();
            
            int responsecode = conn.getResponseCode();
            System.out.println("Response code is: "  responsecode);
            
            if(responsecode != 200)
                throw new RuntimeException("HttpResponseCode: "  responsecode);
            else
            {
                Scanner sc = new Scanner(url.openStream());
                while(sc.hasNext())
                {
                    inline=inline   sc.nextLine();
                }
                System.out.println("\nJSON Response in String format"); 
                System.out.println(inline);
                sc.close();
            }
           
            JSONParser parse = new JSONParser();
                       
            JSONArray jsonarr_1 = (JSONArray)parse.parse(inline);
        
            for(int i=0;i<jsonarr_1.size();i  )
            {
                
                JSONObject jsonobj_1 = (JSONObject)jsonarr_1.get(i);
                System.out.println(jsonobj_1.get("name"));
                
                
            }
                    conn.disconnect();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
 }
  • Related