2

我正在尝试从该 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”找不到。

4

2 回答 2

2

尝试改变这个:

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

有了这个:

String content = object.getJSONObject("page").getString("content");
于 2013-04-24T22:46:07.727 回答
1

而不是使用object.getString("page.content"),试试这个:

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

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

于 2013-04-24T22:46:12.940 回答