0

I have a JSON Arrays of Array like this

{
    "width" : 500,
    "numbers" : [ 
        [46, 11, "1674"],
        [46, 11, "1673"],
        [46, 11, "1677"],
        [46, 11, "1678"],
        [46, 11, "1674"],
        [46, 11,  1673]
    ]
}

And i don't know how to parse it.

JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONArray("numbers");

This code throws a type missmatch error.

1
  • What Json library do you use? It might not be able to cope with the mixed types inside the inner arrays. Commented Sep 8, 2011 at 8:53

1 Answer 1

3

Try GSON, the following should do the trick:

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.google.gson.Gson;

public class ReadTest {

public static void main(String[] args) throws IOException {
    String json = FileUtils.readFileToString(new File("json.txt"));

    Gson gson = new Gson();

    A a = gson.fromJson(json, A.class);

    System.out.println(a.width);
    System.out.println(a.numbers[0][0]);
    }
}


public class A {
    public int width;
    public int numbers[][];
}
Sign up to request clarification or add additional context in comments.

Comments

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.