Java:从JSON解析数组数组
我有像这样的JSON数组arrays
{ "width" : 500, "numbers" : [ [46, 11, "1674"], [46, 11, "1673"], [46, 11, "1677"], [46, 11, "1678"], [46, 11, "1674"], [46, 11, 1673] ] }
我不知道如何解析它。
JSONObject json = new JSONObject(jsonString); JSONArray jsonArray = json.getJSONArray("numbers");
此代码抛出类型不匹配错误。
尝试GSON,以下应该做的诀窍:
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[][]; }