Json字符串更新TextView

时间:2012-04-29 04:05:10

标签: android json android-layout android-intent android-emulator

我想从数据库中提取JSON字符串并更新textview.My Json字符串如下所示:

[
    {
        "meaning": "A boy or young man (often as a form of address)",
        "examples": [
            "I read that book when I was a <em>lad</em>",
            "come in, <em>lad</em>, and shut the door"
        ]
    },
    {
        "meaning": "A group of men sharing recreational, working, or other interests",
        "examples": [
            "she wouldn't let him go out with the <em>lads</em> any more"
        ]
    },
    {
        "meaning": "A man who is boisterously macho in his behavior or actions, esp. one who is interested in sexual conquest",
        "examples": [
            "Tony was a bit of a <em>lad</em>âalways had an eye for the women"
        ]
    },
    {
        "meaning": "A stable worker (regardless of age or sex)",
        "examples": []
    }
]


我只想提取“意思”。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

首先,这是一个JSON数组,其中包含许多meaning

你可以像这样迭代它:

JSONArray jarr = new JSONArray(theJsonString);
for (int i = 0; i < jarr.length(); ++i)
{
    JSONObject jCell = jarr.getJSONObject(i);
    String meaning = cell.getString("meaning");
    // set it as text? concatenate the strings?
}

注意:

  • 为了保持代码清晰,我没有处理异常,请在代码中执行。
  • 详细了解JSON format
  • 以下类是android的一部分:JSONArrayJSONObject
  • 值得一读,下次你不必问:)