※ 引述《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,也比較好操作,不用想太多。
希望有幫助到你