【问题标题】:JSON parsing with Gson JSONObject contains list of JSON Arrays使用 Gson JSONObject 解析 JSON 包含 JSON 数组列表
【发布时间】:2014-08-14 07:11:32
【问题描述】:

如何使用 gson 库将以下 json 响应转换为 Java 对象。 {

"status":"Success",
"result":{
    "VenuereviewDet":[
        {
            "user_profile_imgPath":"",
            "reviewDet_reviewtxt":"New review",
            "reviewDet_reviewsts":"0",
            "reviewDet_name":"jP",
            "reviewDet_reviewdatetime":"Wednesday 23rd of July 2014 07"
        },
        {
            "user_profile_imgPath":"",
            "reviewDet_reviewtxt":"1kjdkgnkdfmgm",
            "reviewDet_reviewsts":"0",
            "reviewDet_name":"sreekanth",
            "reviewDet_reviewdatetime":null
        }
    ],
    "venueimages":[
        {
            "venueimages_thumbimg":"http:\/\/localhost\/app\/images\/videothumb\/JamonJamon_1.jpg",
            "venueimages_image":"http:\/\/localhost\/app\/images\/videothumb\/JamonJamon_1.jpg",
            "venueimages_id":"51",
            "venuedet_id":"10"
        },
        {
            "venueimages_thumbimg":"http:\/\/localhost\/app\/images\/videothumb\/http:\/\/localhost\/app\/images\/videothumb\/JamonJamon_2.jpg",
            "venueimages_image":"http:\/\/localhost\/app\/images\/videothumb\/JamonJamon_2.jpg",
            "venueimages_id":"52",
            "venuedet_id":"10"
        }
    ]
}

}

如何使用 gson 库将此 JSON 响应转换为 Java 对象

【问题讨论】:

    标签: android json parsing gson


    【解决方案1】:

    你需要先制作以下课程

    class VenuereviewDet
        {
            String user_profile_imgPath;
            String reviewDet_reviewtxt;
            String reviewDet_reviewsts;
            String reviewDet_name;
            String reviewDet_reviewdatetime;
        }
    
        class Venueimages
        {
            String venueimages_thumbimg;
            String venueimages_image;
            String venueimages_id;
            String venuedet_id;
        }
        class Data
        {
            ArrayList<VenuereviewDet> VenuereviewDet;
            ArrayList<Venueimages> venueimages;
        }
        class FinalRespone
        {
            String status;
            Data result;
        }
    

    然后

    FinalRespone response=gson.fromJson(jsonResponse,FinalRespone.class);
    

    现在让我解释一下。

    • 在您的响应中有一个名为 result 的对象。因此,在您的班级中,必须有一个可以处理 VenuereviewDet 和 Venueimages 数组的类对象。因为这些键是以数组的形式出现的。

    • SO FinalResponse 是内部包含状态和数据的类。而Data类有两个成员数据,它们只是某些类的ArrayList的对象。

    • 使用 GSON 唯一的问题是创建大量模型类。你在班级结构中打破你的反应的那一刻。这对你来说变得很容易。

    如果您仍然遇到 GSON 问题,请告诉我

    【讨论】:

    • 当我尝试打印 Venueimages 列表大小时出现 NullPointerException。 response.result.Venueimages.size();
    • 编辑了答案。更改了 ArrayList Venueimages;到 ArrayList 场地图像; V 大写。
    • 您必须保持与 api'skey 中的对象名称相同。您也可以 @SerializedName("Key from API") 将变量名和键名分开
    【解决方案2】:

    使用 Google 的 gson,您可以使用这行代码将 json 字符串转换为 java 对象。

    Gson gson = new Gson();
    
    List listObject = gson.fromJson(jsonResponse,ArrayList.class);
    

    YourObject[] objectArray = gson.fromJson(jsonResponse,YourObject[].class);
    

    因为我认为你可能需要“结果”:“成功”你可以有这个结构

    public class MyObject{
    
       private String result
    
       //where you store your VenuereviewDet array and venuimages array
       private ArrayList result;
    
      //getters setters
    
    }
    

    然后转换成java对象

    MyObject objectArray = gson.fromJson(jsonResponse,MyObject.class);
    

    【讨论】:

    • 类结构应该是什么样子的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多