Java 在JSON中检索单个深层数组

Java 在JSON中检索单个深层数组,java,android,json,Java,Android,Json,这里是JSON { "response": { "data": { "images": { "image": [ {... 我想访问图像阵列。我想知道下面的代码是否是最好的方法: JSONObject jsonObjectResponse = new JSONObject(jsonString); JSONObject jsonResponse = jsonObjectResponse.getJSONObject("response");

这里是JSON

{
  "response": {
    "data": {
      "images": {
        "image": [
      {...
我想访问图像阵列。我想知道下面的代码是否是最好的方法:

JSONObject jsonObjectResponse = new JSONObject(jsonString);
JSONObject jsonResponse = jsonObjectResponse.getJSONObject("response");

JSONObject jsonObjectData = new JSONObject(jsonResponse.toString());
JSONObject jsonData = jsonObjectData.getJSONObject("data");

JSONObject jsonObjectImages = new JSONObject(jsonData.toString());
JSONObject jsonImages = jsonObjectImages.getJSONObject("images");

JSONObject jsonObjectImage = new JSONObject(jsonImages.toString());
JSONArray jsonImageArray = jsonObjectImage.getJSONArray("image");
我觉得这需要很多时间来完成一项简单的任务。有人有更“工程化”的解决方案吗?
非常感谢

您不能链接getJSONObject函数吗

JSONObject jsonObjectResponse = new JSONObject("jsonString");
JSONObject jsonImages = jsonObjectResponse.getJSONObject("response").getJSONObject("data")....

不,那不是最好的办法。继续从
JSONObject
s转换为
String
s,然后再次转换回
JSONObject
s。你应该能够做到:

JSONArray jsonImageArray = new JSONObject(jsonString)
    .getJSONObject("response")
    .getJSONObject("data")
    .getJSONObject("images")
    .getJSONArray("image");
即使您只是删除了所有的
toString
调用,它也会变得更加简洁(但我仍然支持上面的示例):

请尝试以下操作:

JSONObject jsonObjectResponse = new JSONObject(jsonString);

JSONArray imageArray = jsonObjectResponse.getJSONObject("response")
     .getJSONObject("data")
     .getJSONObject("images")
     .getJSONArray("image")

希望它能起作用。

我想你可以简单地用以下方式组合所有这些陈述:

JSONObject jsonObjectResponse = new JSONObject(jsonString);
JSONArray jsonImageArray = jsonObjectResponse.getJsonObject("data")
                              .getJsonObject("images")
                              .getJsonArray("image");

您可以使用以下用户链调用:

String jsonString = "{\"response\":{\"data\":{\"images\":{\"image\":[]}}}}";

JSONObject json = new JSONObject(jsonString);

JSONArray image = json.getJSONObject("response") 
                      .getJSONObject("data")
                      .getJSONObject("images")
                      .getJSONArray("image");
例如,您的
图像
对象,如
{“title”:“Some image”,“url”:http://...“}
,因此:

JSONObject firstImage = image.getJSONObject(0);
String url = firstImage.getString("url");
或者你可以使用。你的班级:

class ServerAnswer {
    private Response response;

    // constructors, getters, setters
}

class Response {
    private Data data;

    // constructors, getters, setters
}

class Images {
    @SerializedName("image")
    private Image[] images;

    // constructors, getters, setters
}

class Image {
    private String url, title;

    // constructors, getters, setters
}
和解析:

ServerAnswer answer = new Gson().fromJson(jsonString, ServerAnswer.class);

Image[] images = answer.getResponse().getData().getImages().getImagesArray();
Image firstImage = images[0];
String url = firstImage.getUrl();
就你而言:

   public class Images {
        @SerializedName("image")
        public ArrayList<String> image;
   }

   public class Data {
        @SerializedName("images")
        public Images images;
   }

   public class Response {
        @SerializedName("data")
        public Data data;
   }

   public class MyData {
        @SerializedName("response")
        public Response response;
   }

我认为这是最好的方法

您可以尝试以下代码

JSONArray jsonResponseArray = new JSONObject(strJson).getJSONObject("response").getJSONObject("data").getJSONObject("images").getJSONArray("image");

希望这将对您有所帮助。

尝试Gson解析数据参考此链接非常感谢您的每一个答案,我感谢各位的帮助!上帝保佑,我会试试的,听起来很有趣
MyData data = new Gson().fromJson(yourJsonString, MyData.class);
JSONArray jsonResponseArray = new JSONObject(strJson).getJSONObject("response").getJSONObject("data").getJSONObject("images").getJSONArray("image");