• 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
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject

 
Greenhorn
Posts: 11
  • 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:


Below is the JSON:


Let me know if you need more details.

Thanks,
KP.
 
Bartender
Posts: 15743
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Parsing your JSON yields an array. Then you add it to a new array, so you have an array in an array.

Don't create the new JSONArray. Just cast the parsed object.
 
Kaushal Pandya
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Parsing your JSON yields an array. Then you add it to a new array, so you have an array in an array.

Don't create the new JSONArray. Just cast the parsed object.



Thank you Stephan.
Following advice, I have change the code as below and now it is working for me.



and expected output that I have got:


Thank you
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't "wrong" per se, but can be made more concise:

 
reply
    Bookmark Topic Watch Topic
  • New Topic