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.

michid
  • 10,536
  • 3
  • 32
  • 59
Thommy
  • 5,070
  • 2
  • 28
  • 51
  • What Json library do you use? It might not be able to cope with the mixed types inside the inner arrays. – michid Sep 08 '11 at 08:53

1 Answers1

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[][];
}
lds23
  • 96
  • 5