Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Reading a 'Collection' object from JSON

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

My class has following attributes:
String a;
int b;
ArrayList<String []> c;

I am converting it to JSON object.
jsonObj.put("a", this.a);
jsonObj.put("b", this.b);
jsonObj.put("c", this.c);

I am using Dojo and in my JSP I get the response back in JSON. For ordinary data types 'a' and 'b', it's fine.



But how to get the data that is enclosed in 'c'? How to traverse that collection and get the String[] values?

Please help me with this.

Thanks
Abhishek
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the caveat that I haven't tried this code, it should just be:

data.c[0]

..or...

for (var i = 0; i < data.c.length; i++) {
doSomething(data.c[i]);
}

So long as your JSP rendered the JSON properly, the c should be an array, which you should be able to just access with simple array notation.
 
Abhishek Asthana
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frank

That worked partially. My JSONString looks like this:



So when I do



It prints '[Ljava.lang.String;@128d1f4' rather than the value contained by this object. How do I get the value printed?

Thanks
 
Frank Zammetti
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That *is* the value You have to implement some sort of meaningful toString() on the Java class you're marshalling, otherwise you get the typical, basic Java implementation.
 
catch it before it slithers away! Oh wait, it's a tiny ad:
ScroogeXHTML 8.7 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/730700/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic