-1

Sorry for my limited english first.

I have a JSON sent from a POST request to my spring controller in Java.

I need parse this JSON to a Array. How I can do this?

Thanks!

4
  • 1
    use Google's gson library. And show us some code. Commented Mar 26, 2014 at 10:39
  • 3
    this JSON -- > Which JSON ? Commented Mar 26, 2014 at 10:40
  • @VinayakPingale a JSON sent from a POST request ;) Commented Mar 26, 2014 at 10:42
  • Similar Question: stackoverflow.com/questions/22644334/… Commented Mar 26, 2014 at 11:14

3 Answers 3

0

Another option would be to use json-smart

  1. Convert JSON to an object JSONObject jsonObj = (JSONObject) JSONValue.parseWithException(inputJsonInStringFormat);

  2. Retrieve array by using get() method Integer [] intArray = (JSONArray) (JSONObject) jsonObj.get("arrayName");

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

Comments

0

using javax.json library, it should be something like:

JsonReader reader = Json.createReader(new FileInputStream(dir));
JsonArray re = (JsonObject) reader.read() ;
double[] ret = new double[jre.size()];
for ( int i = 0; i<jre.size(); i++)
    ret[i] = Double.parseDouble( jre.getString(i) );

It does depend, however, on what the Json file looks like. Maybe it's a Json object, with one parameter which has an array as a value, or something. I hope this code can give you a headstart though.

Comments

0

you should not handle it by your-self let spring mvc do it for you, once you enabled spring mvc annotation you will have json converter implicitly, you will need only to add jackson jars to classpath, check this video https://www.youtube.com/watch?v=DnDCJjjSiu8 , your may face problem if want to send JSON as ["value1","value2"], in this case you will need to make custom JsonDeserializer

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.