从Blackberry的Json获取数据

时间:2013-04-24 15:38:25

标签: json blackberry

我想从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":"<p><strong>Lorem Ipsum<\/strong>\u00a0is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#8217;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.<\/p>\n",
        "excerpt":"Lorem Ipsum\u00a0is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#8217;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"
       }
}

以下是我用来从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();
}

它给我一个错误“page.content”无法找到。

2 个答案:

答案 0 :(得分:2)

尝试更改此内容:

String content = object.getString("page.content");

用这个:

String content = object.getJSONObject("page").getString("content");

答案 1 :(得分:1)

不要使用object.getString("page.content"),请尝试以下操作:

    JSONObject page = object.getJSONObject("page");
    String content = page.getString("content");
    Dialog.alert(content);

您只需将流程拆分为两个步骤,首先获取page对象,然后从中检索content