Get data from Json in Blackberry-Collection of common programming errors
i am trying to get content from that Json:
{
"status":"ok",
"page":
{
"id":2,
"type":"page",
"slug":"about-us",
"url":"http:\/\/ugo.offroadstudios.com\/about-us\/",
"status":"publish",
"title":"About Us",
"title_plain":"About Us",
"content":"
Lorem Ipsum\u00a0is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\n",
"excerpt":"Lorem Ipsum\u00a0is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic [...]",
"date":"2012-07-03 09:03:01",
"modified":"2013-02-15 18:20:04",
"categories":[],
"tags":[],
"author":{"id":1,"slug":"sociannel-app","name":"sociannel-app","first_name":"sociannel-app","last_name":"","nickname":"sociannel-app","url":"","description":"not filled yet"},
"comments":[],
"attachments":[],
"comment_count":0,
"comment_status":"closed"
}
}
Here is the code i use to get content from json:
String strURL = "http://ugo.offroadstudios.com/api/get_page/?id=2;deviceside=true";
webConnection wb = new webConnection();
String res = wb.getJson(strURL);
try {
JSONObject object = new JSONObject(res);
if(object.getString("status") == "error")
{
Dialog.alert("Invalid "+object.getString("status"));
}
else
{
String content = object.getString("page.content");
Dialog.alert(content);
RichTextField aboutus = new RichTextField("");
this.setTitle(content);
add(aboutus);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
System.out.println("EX is "+e);
e.printStackTrace();
}
It give me a error “page.content” cannot be found.
-
Instead of using
object.getString("page.content")
, try this:JSONObject page = object.getJSONObject("page"); String content = page.getString("content"); Dialog.alert(content);
You just split the process into two steps, first getting the
page
object, then retrieving thecontent
from that. -
Try changing this:
String content = object.getString("page.content");
with this:
String content = object.getJSONObject("page").getString("content");
Originally posted 2013-11-09 23:14:57.