Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading a 'Collection' object from JSON

 
Abhishek Asthana
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
 
Frank Zammetti
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic