我有一个像这样的 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[][];
}
关于Java:从 JSON 解析数组的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7345359/