1

Is it possible to have JavaScript Array show below in JAVA?

var myArray = [ [‘2011-1-1’, 2],...]

I'm trying to store date and value as a pair in an array. Is there an alternative method in JAVA to do something similar?

codeBarer
  • 2,238
  • 7
  • 44
  • 75

1 Answers1

2

Guess you are talking about keeping/accessing JSON format in Java. You can achieve that using a library like Gson to parse the response into a JsonArray like so,

JsonArray yourArray = new JsonParser()
                          .parse("[[\"2012-14-03\", 2]]")
                          .getAsJsonArray();
// Access your array like so - yourArray.get(0).getAsString();
// yourArray.get(0).getAsInt() etc

Gson Library link - https://code.google.com/p/google-gson/

Does this help?

aravind
  • 2,867
  • 17
  • 28
  • Uh, why on earth go through a parsed String? Just build `JsonElement`s (although Jackson does that better) – fge Mar 19 '14 at 06:22
  • I think the data is obtained from a REST call, which will be a string at the end of the day. Will not be possible to build `JsonElement`s from that, without parsing one way or the other. – aravind Mar 19 '14 at 06:26