Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Help reading JSON array

 
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code reads the "Control" JSON element okay, but gets the following error when reading the Zones array element (the error occurs at the last code line):
java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to [[Ljava.lang.Integer;

I have tried this with both Zones[] and Zones and (Integer and (Integer[]).

Here is the code:


This is the JSON file
{
"Control": "Auto",
"Zones": [1, 2, 3, 4, 5, 6, 7],
"Weekdays": [false,false,false,false,false,false,false],
"ManualZones": [false,false,false,false,false,false,false],
"OnTime": [1,2,0,1]
}

 
Marshal
Posts: 3720
533
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think jsonObject.get() is returning an JSONArray which contains an array of Long objects.

To transform the JSONArray to an long[], you could do something like this: or maybe this:

For example, building on your code:
 
Ranch Hand
Posts: 84
2
Netbeans IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Mr. Mike,

You cannot be converted into integer array directly as mentioned in your code. But you can do the following as one of options:



Output also attached here.

Thanks.
 
Noorul Hameed
Ranch Hand
Posts: 84
2
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mr. Mike,

Output is here as shown in below.

thanks
cmd.png
[Thumbnail for cmd.png]
 
Mike Gauthier
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ron and Noorul !

I took Noorul's code an put it into a new project so I could examine how it works.  Unfortunately, something went wrong because here is my output:
run:

Control: Auto

Zone List: 7
java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to java.lang.Long
at java6.Java6.main(Java6.java:30)

Here is the line of code mentioned (30)/;


I did a direct copy/paste of the code, so what else do I need to do to make it work?
 
Sheriff
Posts: 7111
184
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
Hmm... worked fine for me.  Is schedule.json exactly as you posted?
 
Mike Gauthier
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Hmm... worked fine for me.  Is schedule.json exactly as you posted?


   It is NOW!    

Was changing everything to try to get my code to work and forgot to change it back!

Thank you!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic