Предположим, что у меня есть строка из запроса PHP, подобного этому.Как я могу разобрать строку из PHP на ANDROID, если моя строка не имеет начального значения?
[
{
"id": "1",
"title": "\"Shaira Casandra Libao, the new PSSC President!\"",
"content": "\"Leadership is the key of success\" - Ms. Libao said.\n\nShaira Casandra Libao won the 75% of the vote. ", "penname": "John Ranniel A. Sinel", "date_send": "2015-01-02",
"sender": "John Ranniel Almendares Sinel",
"shortdesc": "The new PSSC President",
"lang": null
}
,
{
"id": "2",
"title": "\"Merleen Mercolita is now the new leader of LGBT community\"",
"content": "\"Being gay is not bad, it's just on man's judgmental mind..\" Ms. Mercolita said during her presscon wherein she admitted that she is a transwomen.",
"penname": "Shaira Casandra Libao",
"date_send": "2015-02-04",
"sender": "Shaira Boncato Libao",
"shortdesc": "LGBT on the go",
"lang": null
}
]
И я хочу, чтобы отобразить только title
ключ первой статьи на струне.
Мой код в андроид:
//The string result variable is the value given by the onPostExecute() method
String title = "";
JSONObject jsonRootObj = new JSONObject(result);
JSONArray jsonArray = jsonRootObj.optJSONArray(whatamIgannaputOnIt);
JSONObject jsonObject = jsonArray.getJSONObject(0);
title = jsonObject.getString("title");
this.kemb.setText(title);
Если у меня есть строка, как это:
{
"Data":
[
{
"id": "1",
"title": "\"Shaira Casandra Libao, the new PSSC President!\"",
"content": "\"Leadership is the key of success\" - Ms. Libao said.\n\nShaira Casandra Libao won the 75% of the vote. ", "penname": "John Ranniel A. Sinel", "date_send": "2015-01-02",
"sender": "John Ranniel Almendares Sinel",
"shortdesc": "The new PSSC President",
"lang": null
}
,
{
"id": "2",
"title": "\"Merleen Mercolita is now the new leader of LGBT community\"",
"content": "\"Being gay is not bad, it's just on man's judgmental mind..\" Ms. Mercolita said during her presscon wherein she admitted that she is a transwomen.",
"penname": "Shaira Casandra Libao",
"date_send": "2015-02-04",
"sender": "Shaira Boncato Libao",
"shortdesc": "LGBT on the go",
"lang": null
}
]
}
Я могу поставить JSONArray jsonArray = jsonRootObj.optJSONArray(whatamIgannaputOnIt);
whatamIgannaputOnIt
переменной строки «Data», но в моем случае, я нет этого! У меня есть строка в верхней части моего вопроса. Итак, как я могу получить правильный вывод с такой строкой?
Возможный дубликат [Как parse JSON в Java] (http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Selvin
Это называется «Json Array». Изучите немного больше об этом. –