作者laiair (大头)
看板AndroidDev
标题Re: [问题] Android 印出JSON资料
时间Wed Apr 22 01:36:52 2015
※ 引述《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://webptt.com/cn.aspx?n=bbs/AndroidDev/M.1429637814.A.57D.html
2F:→ sa1993: 问为甚麽没办法显示在textview 不知如何着手... 04/22 02:58
3F:推 givemepass: 有一种鬼打墙的fu 04/22 12:18
4F:→ hyouun: ppt.cc最近都打不开.. 04/22 15:58
6F:→ sa1993: 错误讯息,还是说这里不能用Textview吗? 04/22 18:38
7F:→ ssccg: 要在main thread才能动view,不能在其他thread直接setText 04/22 19:38
8F:推 givemepass: 突然想到会不会是在其他thread塞字串到textview 04/22 19:39
9F:推 sa1993: 已排解 感谢两位大大 忘了主执行绪不能做改变 不然会出错 04/22 22:20