Let's assume that I have a string from the PHP request like this.
[
{
"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
}
]
And I want to display only the title
key of the first article on the string.
My code in android is:
//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);
If I have a string like this:
{
"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
}
]
}
I can supply the JSONArray jsonArray = jsonRootObj.optJSONArray(whatamIgannaputOnIt);
whatamIgannaputOnIt
variable the string "Data" but on my case, I don't have that! What I have is the string on the top most of my question. So how can I get the correct output with that kind of string?