-1

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?

2 Answers2

0

First, provided JSON String is JSONArray of JSONObject's

So, instead of getting JSONObject from string get JSONArray :

JSONArray jsonRootObj = new JSONArray(result);

Because jsonRootObj JSONArray contains only single JSONObject which is at 0 index, so use 0 to get JSONObject:

JSONObject jsonObj = jsonRootObj.getJSONObject(0);
String title = jsonObj.optString("title"); 

if jsonRootObj JSONArray contains more then one JSONObject then iterate over jsonRootObj to get title from all JSONObject's

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

I'm not sure I understand your question but you should do

JSONArray jsonArray = new JSONArray(result);
for(int i=0; i<jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    String title = jsonObject.getString("title"); 
}
ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77