我想在android中提取给定的json arry,
[{"Outofserviceday":{"outofservice":"2013-02-22"}},
{"Outofserviceday":{"outofservice":"2013-02-27"}},
{"Outofserviceday":{"outofservice":"2013-02-28"}}]
我有用于提取json数据的代码,如下所示
[{"Requestcard":{"id":"994","userprofile_id":"14","userprofile_name":"Syed
Imran","company_name":"DLF Akruti Park, Hinjewadi, Pune,
Maharashtra","sex":"male","travel_date":"2013-02-12"}}]
在这种情况下,我们可以使用代码
来检索json boject JSONObject menuObject = json_data.getJSONObject("Requestcard");
并通过
检索每个元素 requestid= menuObject.getString("id");
但在第一种情况下我们如何识别json数组中的每个Outofserviceday?以及如何提取每个数据???
答案 0 :(得分:1)
你可以做类似下面的事情,可以从字符串json数据创建jsonArray,然后可以在循环中提取json对象。
String json =" [{\"Outofserviceday\":{\"outofservice\":\"2013-02-22\"}}]"; //json-data which is basically a json array
JSONArray jArray = new JSONArray(json); / creating an jsonarray
for (int i = 0; i < jArray.length(); i++) {
// you can have jsonObject from json array here in the loop
}
答案 1 :(得分:1)
试试这个:
JSONObject json = new JSONObject(result);
JSONArray json1= json.getJSONArray("data");
if (json1.length()!=0) {
for (int i = 0; i < json1.length(); i++) {
String name = json1.getJSONObject(i).getString("name");
}
}
答案 2 :(得分:1)
借助以下代码,您可以检索outofservice的值
JSONArray jArray = new JSONArray(your data);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jOutOfServiceDay = jArray.getJSONObject(i);
JSONObject jobj = jOutOfServiceDay.getJSONObject("Outofserviceday");
Log.i("Required data is:", "" + jobj.getString("outofservice"));
}