• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I solve this?


this is my issue:


This is my code



Let me know if you need details

xsi
 
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, at some point you've read a JSONObject from your input, and you're casting it to a JSONArray. A JSONArray is not a JSONObject.

Can you show us the input you're trying to parse?
 
Xsi jadu
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you I solved it

around my Object inside the JSON I forgot
 
Stephan van Hulst
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm glad you figured it out, and thanks for reporting back to us with the answer. Have a cow!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please give the input json file and how you solved this?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

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


public class RWJson
{

@SuppressWarnings("unchecked")
public static void main(String[] args)
{

File parentDir = new File("C://Users//sta2002//Downloads//2022-09-02_81450");

File elements[] = parentDir.listFiles();
for(File element: elements)
{
//System.out.println(element);
File child = new File(element +"//TestResult");
System.out.println(child);

JSONParser jsonParser = new JSONParser();
       
      try(FileReader reader = new FileReader(child +"//statistics.json"))
      {
       
           //Read JSON file
          Object obj = jsonParser.parse(reader);

           JSONArray elementList = (JSONArray) obj;
           System.out.println(elementList);
           
           //Iterate over employee array
           elementList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) );
      }catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       } catch (ParseException e) {
           e.printStackTrace();
       }

}
}



public static void parseEmployeeObject(JSONObject employee)
   {
       //Get employee object within list
       JSONObject employeeObject = (JSONObject) employee.get("Total");
       
       //Get employee first name
       String firstName = (String) employeeObject.get("meanResTime");    
       System.out.println(firstName);
       
       //Get employee last name
       String lastName = (String) employeeObject.get("throughput");  
       System.out.println(lastName);
       
   }

}



I'm also getting the same kind of error.
Can you tell me how to solve this?


 
Greenhorn
Posts: 4
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this is 5y old, but it is astounding how few online code examples actually work to go from JSON to CSV.

Although this does look like a path/jar issue in the stacktrace, it was in the source, as the OP pointed out. But they didn't explain in a way that is useful.

This is from an API, and will cause the error:


The above failed. When I simply wrapped the entire contents in a pair of brackets ("[ ...]"), though, it was all a single line, not discernable as CSV.

However, when I trimmed the characters before AND after the existing outer brackets, like this:

joy.

No header values, but that is a separate issue. To help those running recent java, here is a snippet that works:



HTH

I was on here for a million years when it was the java ranch... starting over.. oh well!
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic