Arhn - архитектура программирования

Модернизация проблем с разбором JSON при извлечении значения из списка? Как решить?

Например, у меня есть 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 равны нулю, например, из приведенного выше кода строковые значения a, b, c, все они возвращают значение как null. Может ли кто-нибудь помочь найти и решить это проблема?.

15.04.2018

  • можете ли вы поделиться полным ответом, чтобы я мог проверить их POJO 15.04.2018
  • Привет, E/Response: [com.jbn.assignment.model.AnnouncementTitle@15019a9, com.jbn.assignment.model.AnnouncementTitle@38a29b2e, com.jbn.assignment.model.AnnouncementTitle@1c1846cf, com.jbn.assignment.model .AnnouncementTitle@2f03685c, com.jbn.assignment.model.AnnouncementTitle@2167a765, com.jbn.assignment.model.AnnouncementTitle@2b413c3a, com.jbn.assignment.model.AnnouncementTitle@19fc80eb, com.jbn.assignment.model.AnnouncementTitle @ 208cae48. Всего в ответе 26 элементов. 15.04.2018
  • Покажите свой базовый класс ответа 15.04.2018

Ответы:


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;
    }
15.04.2018

2

Получить объект 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")
);
15.04.2018

3

скопируйте весь ответ 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());
    }
});
13.02.2020
Новые материалы

Коллекции публикаций по глубокому обучению
Последние пару месяцев я создавал коллекции последних академических публикаций по различным подполям глубокого обучения в моем блоге https://amundtveit.com - эта публикация дает обзор 25..

Представляем: Pepita
Фреймворк JavaScript с открытым исходным кодом Я знаю, что недостатка в фреймворках JavaScript нет. Но я просто не мог остановиться. Я хотел написать что-то сам, со своими собственными..

Советы по коду Laravel #2
1-) Найти // You can specify the columns you need // in when you use the find method on a model User::find(‘id’, [‘email’,’name’]); // You can increment or decrement // a field in..

Работа с временными рядами спутниковых изображений, часть 3 (аналитика данных)
Анализ временных рядов спутниковых изображений для данных наблюдений за большой Землей (arXiv) Автор: Рольф Симоэс , Жильберто Камара , Жильберто Кейрос , Фелипе Соуза , Педро Р. Андраде ,..

3 способа решить квадратное уравнение (3-й мой любимый) -
1. Методом факторизации — 2. Используя квадратичную формулу — 3. Заполнив квадрат — Давайте поймем это, решив это простое уравнение: Мы пытаемся сделать LHS,..

Создание VR-миров с A-Frame
Виртуальная реальность (и дополненная реальность) стали главными модными терминами в образовательных технологиях. С недорогими VR-гарнитурами, такими как Google Cardboard , и использованием..

Демистификация рекурсии
КОДЕКС Демистификация рекурсии Упрощенная концепция ошеломляющей О чем весь этот шум? Рекурсия, кажется, единственная тема, от которой у каждого начинающего студента-информатика..