Java:从JSON解析数组数组 [英] Java: parse Array of Arrays from JSON
本文介绍了Java:从JSON解析数组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有像这样的JSON数组阵列
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]
]
}
我不知道如何解析它。
JSONObject json = new JSONObject(jsonString);
JSONArray jsonArray = json.getJSONArray("numbers");
此代码抛出类型不匹配错误。
This code throws a type missmatch error.
推荐答案
尝试GSON,以下应该可以解决问题:
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[][];
}
这篇关于Java:从JSON解析数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文