0

AndroidでこのJSONファイルを正しく解析するにはどうすればよいですか?

たとえば、タワー 1 (そこには異なる量の平日が存在する可能性があります) からのすべてのオブジェクトと、それらの平日の異なる量の時間枠が必要です。

次のような静的メソッドを正常に使用しました

JSONObject jArray = ja.getJSONObject("towers").getJSONObject("tower1") .getJSONObject("tuesday").getJSONObject("11:45-12:20");

そして、getString() メソッドを使用して value1 の値を取得しました。

しかし、多くの可能性があるため、それらは動的である必要があります。

{
"towers": {
"tower 1": {
  "tuesday": {
    "07:30-11:30": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3"
    },
    "11:45-12:20": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3"
    }
  }
},
"tower 2": {
  "wednesday": {
    "07:15-11:35": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3"
    },
    "12:45-15:10": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3"
    },
    "15:30-17:05": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3"
    }
  },
  "tuesday": {
    "07:15-11:35": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3"
    },
    "12:45-15:10": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3"
    },
    "14:25-17:05": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3"
    }
  }
}
},
"building": {
"building 1": {
  "monday": {
    "07:15-12:20": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3",
      "value4": "test value 4"
    }
  },
  "tuesday": {
    "07:15-11:35": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3",
      "value4": "test value 4"
    }
  },
  "wednesday": {
    "07:15-11:35": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3",
      "value4": "test value 4"
    }
  },
  "friday": {
    "07:15-11:35": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3",
      "value4": "test value 4"
    }
  }
},
"building 2": {
  "saturday": {
    "08:05-11:00": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3",
      "value4": "test value 4"
    }
  }
},
"building 3": {
  "monday": {
    "12:45-15:10": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3",
      "value4": "test value 4"
    }
  },
  "tuesday": {
    "08:55-11:35": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3",
      "value4": "test value 4"
    },
    "15:30-17:55": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3",
      "value4": "test value 4"
    }
  },
  "thursday": {
    "07:15-09:40": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3",
      "value4": "test value 4"
    },
    "10:00-12:25": {
      "value1": "test value 1",
      "value2": "test value 2",
      "value3": "test value 3",
      "value4": "test value 4"
    }
  }
}
}
}
4

2 に答える 2

1

json 文字列キーが動的な場合は、次のように解析できます。

JSONObject root = new JSONObject(yourString);
// get towers JSONObject
JSONObject towers = root.getJSONObject("towers"); 
// get all Towers name from towers JSONObject 

JSONArray alltowerslist=towers.names();

for(int i=0;i<alltowerslist.length();i++){
   // get sub towers from towers
   JSONObject sub_towers = towers.getJSONObject(alltowerslist.optString(i)); 
   // get days list from sub_towers
    JSONArray alldayslist=sub_towers.names();
    for(int j=0;j<alldayslist.length();j++){
       // get days from  sub_towers
        JSONObject days_json = sub_towers.getJSONObject(alldayslist.optString(j)); 

         // get time json JSONObject from days_json
        JSONArray alltimeslist=days_json.names();
        for(int k=0;k<days_json.length();k++){
       // get time from  sub_towers
        JSONObject time_json = days_json.getJSONObject(alltimeslist.optString(k));

        // now get all value1 from time_json
        JSONArray allvalelist=time_json.names();
        for(int l=0;l<time_json.length();l++){
           String str_value = time_json.optString(allvalelist.optString(l));
          }   
        }
    }
}
于 2013-05-30T08:08:57.560 に答える
1

ネストされたjsonオブジェクトの応答解析のこのリンクを見てください....

http://yodi.polatic.me/example-parse-nested-json-array-and-object-java/

于 2013-05-30T08:04:20.423 に答える