你好,我是 android studio 的新手,我目前正在尝试弄清楚如何解析这个 json 文件:http://stman1.cs.unh.edu:6191/games进入二维数组。我似乎无法找到一个简单的方法来做到这一点。
我不确定 JsonObject 返回对象类型的确切原因。我知道它无法转换为 int,因为它可能是一个 int 数组,但我也无法获取该数组,因为它没有像外部数组那样的名称。
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("grid");
for( int i=0; i < jsonArray.length(); i++ ){
JSONObject num = jsonArray.getJSONObject(i);
gridVals[i] = num.getInt();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
最佳答案
这是一个简单的示例,如果您使用Gson库,可以直接使用。
class Response{
List<List<Integer>> grid= new ArrayList<>();
}
public class ParsingJson {
public static void main(String[] args) {
// data is your json string that you need to provide here
Response response = (new Gson()).fromJson(data, Response.class);
System.out.println(response.grid.size());
for (List<Integer> integers : response.grid) {
for (Integer integer : integers) {
System.out.println(integer);
}
}
}
}
我尝试使用 Gson library 读取您的 json 数据
关于java - 如何解析二维json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57917249/