• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in JSONArray Initialization  RSS feed

 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,
Why would does this code work

And this not work.

I get the exception “JSONArray initial value should be a string or collection or array”. The transportDetails is an array, I guess I'm not sure what is going wrong. Can anyone help me out?
PS: In both the cases the transportDetails has value in it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Impossible to say without knowing what library you're using.
 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan,

I'm using the JSON Library. I feel the problem was that the, inside the new JSONArray was being considered as an object and not as a String (Hence the error message "JSONArray initial value should be a string or collection or array"). So when I added .toString() at the end, the code is working. Not sure if it's a correct hypothesis, but please do correct me if I am wrong.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation really says it all. The constructor that takes an object actually expects it to be an array of some sort. A JSONArray is not an array, so that's why you get the exception.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!