0

我想提取从数据库的JSON字符串和更新textview.My JSON字符串看起来是这样的:
JSON字符串更新的TextView

[ 
    { 
     "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

[** ** jsonlint(http://jsonlint.com/)是一个用于格式化JSON所以它是清晰的一个很好的工具。我继续为您格式化您的JSON。 – 2012-04-29 04:15:49

+0

谢谢@PeterAjtai :) – Maxsteel 2012-04-29 04:19:43

+0

不客气!如果能够解决您的问题,请不要忘记接受答案。 (复选框大纲下面的答案票) – 2012-04-29 04:23:13

回答

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的一部分:JSONArray,JSONObject
  • 值得一读的,下次你就不会要问:)
+0

非常感谢。 :) – Maxsteel 2012-04-29 04:20:24