繁体   English   中英

Java:从JSON解析数组数组

[英]Java: parse Array of Arrays from JSON

我有这样的JSON数组数组

{
    "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[][];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM