Получение строкового значения из объекта Json Android

Я новичок в Android. В моем проекте я получаю следующий json из HTTP-ответа.

[{"Date":"2012-1-4T00:00:00",
"keywords":null,
"NeededString":"this is the sample string I am needed for my project",
"others":"not needed"}]

Я хочу получить« NeededString » "из приведенного выше json. Как это получить?

28
задан DeepBlue 16 February 2019 в 12:32
поделиться

1 ответ

См. мой ответ ниже, вдохновленный ответами выше, но немного более подробный...

// Get The Json Response (With Try Catch)
try {

    String s = null;

    if (response.body() != null) {

        s = response.body().string();

        // Convert Response Into Json Object (With Try Catch)
        JSONObject json = null;

        try {
            json = new JSONObject(s);

            // Extract The User Id From Json Object (With Try Catch)
            String stringToExtract = null;

            try {
                stringToExtract = json.getString("NeededString");

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

} catch (IOException e) {
    e.printStackTrace();
}
0
ответ дан 28 November 2019 в 02:30
поделиться
Другие вопросы по тегам:

Похожие вопросы: