Как проанализировать список объектов Json с помощью Retrofit Api - PullRequest
0 голосов
/ 10 октября 2018
{
    "0": {
        "id": "1",
        "img": "ginterests/1.png",
        "title": "Training & Educations",
        "description": "Yoga | Fitness | Daily Trainings | Dance | Music +",
        "follow": 1
    },
    "1": {
        "id": "2",
        "img": "ginterests/2.png",
        "title": "Consultations",
        "description": "Legal | Studies | Entrance Exam Preparations +",
        "follow": 1
    },
    "2": {
        "id": "3",
        "img": "ginterests/3.png",
        "title": "Public Events",
        "description": "City Events | Trending | Current Events +",
        "follow": 0
    },
    "3": {
        "id": "4",
        "img": "ginterests/4.png",
        "title": "Business Events",
        "description": "Talks & Shows | Press | Live coverage | Business meets +",
        "follow": 0
    },
    "4": {
        "id": "5",
        "img": "ginterests/5.png",
        "title": "Performances",
        "description": "Regional & Global | Celebreties | Standup +",
        "follow": 0
    },
    "5": {
        "id": "6",
        "img": "ginterests/6.png",
        "title": "News & Entertainment",
        "description": "Regional & Global | News | Trending +",
        "follow": 0
    },
    "6": {
        "id": "7",
        "img": "ginterests/default.png",
        "title": "Celebrities",
        "description": "City Events | Trending | Current Events +",
        "follow": 0
    },
    "success": true,
    "message": "Interests data sent",
    "interestsCount": 7
}

1 Ответ

0 голосов
/ 10 октября 2018

Анализ объекта JSON и получение из него отдельных данных очень просто.Просто выполните следующие шаги:

  1. Сохраните полный ответ как строковое значение: final String response= response.body().string();
  2. Инициализируйте объект JSON: JSONObject resultObject = null;
  3. Теперь запустите приведенный ниже код для анализа данных из вашего объекта JSON: `try {

                resultObject = new JSONObject(response);
                JSONObject result = resultObject.getJSONObject(0);
                final String id= volumeObject.getString("id"); //repeat this step to get all the data from the JSON object 
            } catch (JSONException e) {
                e.printStackTrace();
            }`
    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...