Re: [問題] Android 印出JSON資料

看板AndroidDev作者 (大頭)時間10年前 (2015/04/22 01:36), 編輯推噓4(405)
留言9則, 4人參與, 最新討論串2/2 (看更多)
※ 引述《sa1993 (竹)》之銘言: : http://ppt.cc/fahA 原始網址 : http://ppt.cc/3btK 有標顏色 : 小弟請問各位前輩 像這種前後有別的東西混合(紅色標記的) : 該怎讀取我要的"records"底下的值(藍色標記的) : 部分code : JSONObject mTitle = new JSONObject(JsonText); //網址讀到的放到這 : JSONArray arrayJson=mTitle.getJSONArray("result"); : try { : for(int i = 0 ; i <arrayJson.length() ;i++){ : t1.setText("name:"+arrayJson.getJSONObject(i).getString("Name").toString()); : } : }catch (JSONException e) { : e.printStackTrace(); : } 原Po給的json字串長這樣: { "result": { "resource_id": "c57f54e2-8ac3-4d30-bce0-637a8968796e", "fields": [ { "type": "int4", "id": "_id" }, { "type": "text", "id": "Name" }, { "type": "text", "id": "Sex" } ] } } result 是 JSONObject 沒錯。 包含了一個 resource_id 的屬性,和 fields 的 JSONArray JSONArray 想成 JSONObject陣列就好。 JSONArray arrayJson=mTitle.getJSONObject("result").getJSONArray("fields"); for(int position = 0; position < arrayJson.length(); position++) { JSONObject jsonObj = arrayJson.getJSONObject(position); String json = jsonObj.toString(); String type = jsonObj.getString("type"); String id = jsonObj.getString("id"); } 我是後來都用gson,也比較好操作,不用想太多。 希望有幫助到你 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.45.228.162 ※ 文章網址: https://www.ptt.cc/bbs/AndroidDev/M.1429637814.A.57D.html

04/22 02:57, , 1F
04/22 02:57, 1F

04/22 02:58, , 2F
問為甚麼沒辦法顯示在textview 不知如何著手...
04/22 02:58, 2F

04/22 12:18, , 3F
有一種鬼打牆的fu
04/22 12:18, 3F

04/22 15:58, , 4F
ppt.cc最近都打不開..
04/22 15:58, 4F

04/22 18:36, , 5F

04/22 18:38, , 6F
錯誤訊息,還是說這裡不能用Textview嗎?
04/22 18:38, 6F

04/22 19:38, , 7F
要在main thread才能動view,不能在其他thread直接setText
04/22 19:38, 7F

04/22 19:39, , 8F
突然想到會不會是在其他thread塞字串到textview
04/22 19:39, 8F

04/22 22:20, , 9F
已排解 感謝兩位大大 忘了主執行緒不能做改變 不然會出錯
04/22 22:20, 9F
文章代碼(AID): #1LDegsLz (AndroidDev)
文章代碼(AID): #1LDegsLz (AndroidDev)