【问题标题】:Json string to update TextView用于更新 TextView 的 Json 字符串
【发布时间】:2012-04-29 04:05:10
【问题描述】:

我想从数据库中提取一个 JSON 字符串并更新一个 textview。我的 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": []
    }
]


我只是想提取“意义”。我该怎么做呢?

【问题讨论】:

  • jsonlint 是一个很好的格式化 JSON 的工具,因此它清晰易读。我继续为你格式化了你的 JSON。
  • 不客气!如果它解决了您的问题,请不要忘记接受答案。 (答案投票下方的复选框大纲)
  • 这个网站的新手。不知道。爱这里的每一刻:)

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


【解决方案1】:

首先,这是一个 JSON 数组,里面有很多 meanings。

你可以这样迭代它:

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
  • 值得一读,下次不用问了:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2021-12-10
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多