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