Daniel Omara Daniel Omara - 4 months ago 10
JSON Question

How to parse nested Objects in android to readable html

I have this code from

JSONParser.java


try{
JSONArray postArray = jsonObject.getJSONArray("posts");
// Go through each post
for (int i = 0; i < postArray.length(); i++) {
JSONObject postObject = postArray.getJSONObject(i);

Post post = new Post();
post.setCfs(postObject.getJSONObject("custom_fields").optString("entity", "N/A"));


that displays this

["Some text"]


I want it to display without
["
and
"]


My Json for that specific part is as below

"posts":[{
"date":"2016-02-10 10:28:42",
"categories":[{}],
"tags":[],
"author":{"name":"admin"},
"custom_fields":{
"ref_number":["ITB NUMBER: ITB\/002\/2016"],
"deadline":["26 February, 2016"],
"entity":["Refugees (xxx)"]
}

Answer

It seems that "entity" is array and you should try something like that:

post.setCfs(postObject.getJSONObject("custom_fields").optJSONArray("entity").getString(0));