Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am I getting this error ?? java.lang.ClasscastException

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 json.simple@1.1 of loader 'app')
    at lamda_pi/lamda_pi.letsRead.main(letsRead.java:77)

Sample json looks like below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
{
"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();
        }
    }
 }

>Solution :

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();
        }
    }
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading