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?

6
  • You need to try JSON objects . Commented Mar 19, 2014 at 6:00
  • 1
    You mean a nested array? Sure all languages have this. Btw, you're not using the right quotes ’ != ' Commented Mar 19, 2014 at 6:01
  • 1
    Why would you need Javascript arrays in Java, Java has its own arrays. Make an array of Date objects. Unless you mean to ask how parse JavaScript arrays in Java? Commented Mar 19, 2014 at 6:01
  • Use Map in java with key/value pair Commented Mar 19, 2014 at 6:02
  • I'm getting data through REST into JAVA, where I do some data manipulation. I wanted to keep the date and integer value together. Commented Mar 19, 2014 at 6:04

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Uh, why on earth go through a parsed String? Just build JsonElements (although Jackson does that better)
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 JsonElements from that, without parsing one way or the other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.