在从列表中检索值时改进JSON解析问题?怎么解决?

时间:2018-04-15 04:05:47

标签: android json retrofit2

我有一个NESTED JSON来解析,例如;

 [
{
    "ID": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "68613",
        "IsBinaryUnique": true
    },
    "NATIVE_DATE": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "2015/02/22",
        "IsBinaryUnique": true
    },
    "ANNOUNCEMENT_DATE": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "February 22,  2015",
        "IsBinaryUnique": true
    },
    "EXPIRY": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "",
        "IsBinaryUnique": true
    },
    "ANNOUNCEMENT_DESCRIPTION": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "",
        "IsBinaryUnique": true
    },
    "ANNOUNCEMENT_TITLE": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "Emirates Motor Company unveils all-new Mercedes-Benz 2015 C-Class",
        "IsBinaryUnique": true
    },
    "ANNOUNCEMENT_IMAGE": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "HTTP://94.56.199.34/EMC/EMCPortal/DesktopModules/Farabi.ICatalog/CatalogImages/488/C-2048_sd-eng.jpg",
        "IsBinaryUnique": true
    },
    "ANNOUNCEMENT_IMAGE_THUMBNAIL": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "http://94.56.199.34/EMC/IPDP/thumbnail.ashx?File=/DesktopModules/Farabi.ICatalog/CatalogImages/488/C-2048_sd-eng.jpg",
        "IsBinaryUnique": true
    },
    "ANNOUNCEMENT_HTML": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "<!--NEWS-->\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n    <meta name=\"viewport\" content=\"width=device-width\" />\r\n    <title>News & Promotions</title>\r\n    <style type=\"text/css\">\r\n        H1\r\n        {\r\n            text-align: left;\r\n            color:Black;\r\n            font-size:22px;\r\n            font-weight: bold;\r\n            font-family:Arial;\r\n        }\r\n        Body\r\n        {\r\n            background-color: transparent\";\r\n            font-family:Arial;\r\n        }\r\n        .Header\r\n        {\r\n            -webkit-border-top-left-radius: 10px;\r\n            -webkit-border-bottom-left-radius: 10px;\r\n            -moz-border-radius-topleft: 10px;\r\n            -moz-border-radius-bottomleft: 10px;\r\n            border-top-left-radius: 10px;\r\n            border-bottom-left-radius: 10px;\r\n            background-color: #0e100f\";\r\n            margin-top: 10px;\r\n            font-weight: bold;\r\n            border: 5 none transperant;\r\n            font-family:Arial;\r\n        }\r\n        .SubTitle\r\n        {\r\n            color: black;\r\n            font-weight: bold;\r\n            font-size:16px;\r\n            font-family:Arial;\r\n        }\r\n        \r\n        .ContentText\r\n        {\r\n            color: black;\r\n            font-size:16px;\r\n            font-family:Arial;\r\n        }\r\n        \r\n        .Content\r\n        {\r\n            margin-left: 5px;\r\n        }\r\n    </style>\r\n</head>\r\n<body leftmargin=\"0\" topmargin=\"0\">\r\n    <table border=\"0\" cellpadding=\"5\" cellspacing=\"5\" >\r\n        <tr>\r\n            <td>\r\n                <h1>\r\n                    Emirates Motor Company unveils all-new Mercedes-Benz 2015 C-Class\r\n                </h1>\r\n             </td>\r\n        </tr>\r\n        <tr class=\"SubTitle\">\r\n             <td align=\"right\">\r\n                  February 22,  2015\r\n              </td>\r\n        </tr>\r\n        <tr class=\"SubTitle\">\r\n             <td align=\"right\">\r\n                 Valid Till &nbsp $Expired_Date\r\n              </td>\r\n        </tr>\r\n        <tr>\r\n            <td>\r\n                                <img alt=\"\" src=\"http://94.56.199.34/EMC/EMCPortal/DesktopModules/Farabi.ICatalog/CatalogImages/488/C-2048_sd-eng.jpg\" style=\"width: 100%; float: right\" hspace=\"0\" />\r\n                </td>\r\n        </tr>\r\n        <tr>\r\n            <td>     \r\n                <div class=\"Content\" valign=\"top\">\r\n                    <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\r\n                        <tr>\r\n                            <td class=\"ContentText\" align=\"left\">\r\n                                <p><span>Emirates Motor Company (EMC), General Distributors of Mercedes-Benz in the Emirate of Abu Dhabi and the flagship company of Al Fahim Group, launched the all-new Mercedes-Benz 2015 C-Class in a gala iftar and a superb unveiling of the fabulous new innovative car on Saturday, 5 July at their close-to-heart Airport Road Showroom in Abu Dhabi.</span></p>\r\n<p><span>In the presence of newly appointed CEO Automotive of Al Fahim Group, Mr. Frank Bernthaler, the celebrative evening continued with a press conference where EMC hosts Mr. Kamal Rafih, General Manager, Mr. Jordan Duckett, General Sales Manager, and Mr. Rabih Eid, Operations Manager, were head-table spokespersons.</span></p>\r\n<p><span>\"We are delighted to be revealing the admirable all-new Mercedes-Benz 2015 C-Class during a blessed, festive month and announce the great achievements we've accomplished so far this year,\" praised Mr. Rafih in his speech to the media. \"As we conclude the first half of the year, we also celebrate a well-earned increase in our sales performance of 42% compared to the same period last year,\" he added.</span></p>\r\n<p><span>Among the most popular models in 2014 which witnessed an exponential rise were the legendary Mercedes-Benz S-Class by an impressive 347%, the sporty A-Class by 156%, the luxurious E-Class by 46%, and the entire sports utility vehicle (SUV) range by 22%, which includes the G-Class, GL-Class, GLK Class and the M-Class.</span></p>\r\n<p><span>During the memorable occasion, media representatives and guests were also invited to explore the latest innovation by Mercedes-Benz and register for the driving experience. The all-new C-Class sets new standards in the premium medium-size category in terms of striking appearance, cutting-edge technology, spacious comfort and refined sportiness. A host of new assistance systems offers safety of the highest standard, while a new optional air-sprung suspension provides for exemplary ride and driving comfort as well as nimble and agile handling.</span></p>\r\n<p><span>Thanks to intelligent and innovative lightweight construction, the aluminum hybrid body is around 70 kilograms lighter than a conventional steel body. Compared with the successful predecessor, the aluminum content has increased from less than 10% to almost 50%. As a result, the vehicle's overall weight is reduced by up to 100 kilograms. Furthermore, with an 80-millimeter increase in the wheelbase compared with the previous model, the vehicle is 95 millimeters longer and 40 millimeters wider. The result is a more spacious interior.</span></p>\r\n<p><strong><span>High-class interior with fine details</span></strong><span><br />\r\nThe innovative touchpad developed by Mercedes-Benz in the hand-rest marks a further evolutionary step. As on a smartphone, this provides for simple and intuitive operation of all the head-unit functions using finger gestures. The touchpad also permits letters, numbers and special characters to be entered in handwriting - in any language supported by Audio 20. One of the main highlights of the innovative touchpad is that Arabic letters, numbers and even special characters are supported, and can be entered into the handwriting recognition function.</span></p>\r\n<p><strong><span>Mercedes-Benz Intelligent Drive: the intelligent car</span></strong><span><br />\r\nThe new C-Class incorporates almost all of the new assistance systems with a host of enhanced functions which celebrated their world premiere in the S-Class and E-Class only some months ago. The assistance systems enhance both comfort and safety. Mercedes-Benz calls this Intelligent Drive.</span></p>\r\n<p><span>The new C-Class offers numerous innovative safety and assistance systems. It is equipped as standard with ATTENTION ASSIST, which can warn the driver of inattentiveness and fatigue. In addition to Adaptive Brake Assist, which offers protection from collisions from speeds as low as 7 km/h, standard-fit COLLISION PREVENTION ASSIST PLUS also features an additional function: when the risk of collision persists and the driver fails to respond, the system is able to carry out autonomous braking at speeds of up to 200 km/h, thereby reducing the severity of collisions with slower or stopping vehicles. The system also brakes in response to stationary vehicles at a speed of up to 50 km/h, and is able to prevent rear-end collisions at up to 40 km/h.</span></p>\r\n<p><strong><span>Other safety features include:</span></strong> <span><br />\r\n-DISTRONIC PLUS with Steering Assist and integrated Stop&amp;Go Pilot\r\n<br />\r\n-BAS PLUS Brake Assist and PRE-SAFE&reg; Brake\r\n<br />\r\n-Enhanced Active Lane Keeping Assist\r\n<br />\r\n-Active Parking Assist\r\n<br />\r\n-Traffic Sign Assist\r\n<br />\r\n-Adaptive Highbeam Assist Plus</span></p>\r\n                            </td>\r\n                        </tr>\r\n                    </table>\r\n                </div>\r\n            </td>\r\n        </tr>\r\n    </table>\r\n</body>\r\n</html>\r\n",
        "IsBinaryUnique": true
    }
},
{
    "ID": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "66055",
        "IsBinaryUnique": true
    }, etc....}]

我需要获取公告标题值,为此,我创建了一个公告标题的模型类。

public class AnnouncementTitle {


@SerializedName("Tag")
private String tag;

@SerializedName("TypeCode")
private String typecode;

@SerializedName("Value")
private String value;

@SerializedName("IsBinaryUnique")
private boolean isbinary;

public AnnouncementTitle(String tag, String typecode, String value, boolean isbinary) {
    this.tag = tag;
    this.typecode = typecode;
    this.value = value;
    this.isbinary = isbinary;
}

public String getTag() {
    return tag;
}

public void setTag(String tag) {
    this.tag = tag;
}

public String getTypecode() {
    return typecode;
}

public void setTypecode(String typecode) {
    this.typecode = typecode;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public boolean isIsbinary() {
    return isbinary;
}

public void setIsbinary(boolean isbinary) {
    this.isbinary = isbinary;
}

}

我的API类就像,

public interface Api {

String BASE_URL ="base url";
@GET("url to get values from")
Call<ArrayList<AnnouncementTitle>> getData();}

我已在MainActivity.java

中实施了改装调用
 public void getData() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Api.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    Api api = retrofit.create(Api.class);

    Call<ArrayList<AnnouncementTitle>> call = api.getData();

    call.enqueue(new Callback<ArrayList<AnnouncementTitle>>() {


        @Override
        public void onResponse(Call<ArrayList<AnnouncementTitle>> call, Response<ArrayList<AnnouncementTitle>> response) {



            ArrayList<AnnouncementTitle> response_array = response.body();

            Log.e("Response "," "+ response_array);
            Log.e("Response "," "+ response_array.size());

            Log.e("Response ","Value "+ response_array.get(0).getValue());


            for (int i = 0;i<response_array.size();i++){
                AnnouncementTitle announcementTitle= response_array.get(i);
                String a = announcementTitle.getTag();
                String b = announcementTitle.getValue();
                String c = announcementTitle.getTypecode();

                //Log.e("Response ","Value "+ b);

                // Log.e("Response ","Value "+ c);
            }




        }

        @Override
        public void onFailure(Call<ArrayList<AnnouncementTitle>> call, Throwable t) {

            Log.e("Failed "," ");
        }
    });
}}

我的问题是我正确地将响应作为数组列表获取,但是ArrayList中的值为null,例如从上面的代码中字符串值a,b,c,所有这些值都返回null为null。任何人都可以帮助查找并解决这个问题?。

3 个答案:

答案 0 :(得分:1)

首先:您显示的JSON无效。如果您声明

,除了无效的大括号放置
@SerializedName("TypeCode")
private String typecode;

你会有

"TypeCode": "5"

而不是

"TypeCode": 5
JSON中的

。代表您的JSON的正确ArrayList<AnnouncementTitle>是:

[
{"ID": {
    "Tag": "string",
    "TypeCode": "5",
    "Value": "68613",
    "IsBinaryUnique": true}
},

{"NATIVE_DATE": {
    "Tag": "string",
    "TypeCode": "5",
    "Value": "2015/02/22",
    "IsBinaryUnique": true}
},

{"ANNOUNCEMENT_TITLE": {
    "Tag": "string",
    "TypeCode": "5",
    "Value": "Title of announcement",
    "IsBinaryUnique": true}
}
]

第二:您的请求和响应具有相同的结构 - ArrayList<AnnouncementTitle>。您正在发送此列表并将其作为服务器答案进行格式化。这真的是你想做的事吗?

第三次:如果您真的希望ArrayList<AnnouncementTitle>作为服务器答案,为什么要手动解析

for (int i = 0;i<response_array.size();i++){
    ...
}

?最好强迫Retrofit为你做这件事:

call.enqueue(new Callback<ArrayList<AnnouncementTitle>>() {
    @Override
    public void onResponse(Call<ArrayList<AnnouncementTitle>> call, ArrayList<AnnouncementTitle> list) {
        ArrayList<AnnouncementTitle> response_list = list;
    }

答案 1 :(得分:0)

从列表中获取JSON对象

JSONObject obj = responseArray.get(i);
JSONObject ID = obj.getJSonObject("ID");

模型类中的变量应该是这样的(我想你忘了设置      TypeCode声明Integer)

@SerializedName("Tag")
private String tag;

@SerializedName("TypeCode")
private int typecode;

@SerializedName("Value")
private String value;

@SerializedName("IsBinaryUnique")
private boolean isbinary;

将数据传递到类似于for循环的模型类

AnnouncementTitle  model = new AnnouncementTitle(
   ID.getString("TAG"),
   ID.getInt("TypeCode"),
   ID.getString("Value"),
   ID.getBoolean("IsBinaryUnique")
);

答案 2 :(得分:0)

复制所有json响应并将其从链接更改为pojo类 https://www.jsonutils.com/ 并在类中创建get set方法

您的界面将变成这样

public interface Api {

@GET("url to get values from")
Call<PojoClassName> getData();
}

改造类如下

Api api = retrofit.create(Api.class);

Call<PojoClassName> call = api.getData();

call.enqueue(new Callback<PojoClassName>() {


    @Override
    public void onResponse(Call<PojoClassName> call, Response<PojoClassName> response) {
        Log.e("Response "," "+response.body().toString);
        Log.e("Response "," "+response.body().getID().getTag());
        Log.e("Response "," "+response.body().getEXPIRY().getTag());
    }

    @Override
    public void onFailure(Call<PojoClassName> call, Throwable t) {

        Log.e("Failed ",t.getMessage());
    }
});