本文介绍了Android解析JSONObject内部的JSONObjects的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Android中正确解析此JSON文件?

How can I parse this JSON file correctly in Android?

我需要塔1中的所有对象(那里可能有不同数量的工作日)以及这些工作日中不同的时间范围.

I need all the objects from lets say tower 1 (there can be different amounts of weekdays in there) and also different amount of time frames in those weekdays.

我已经成功使用了静态方法,例如

I've successfully used a static method like

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

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

然后我使用getString()方法获得了value1的值.

And then I got the value for value1 using the getString() method.

但是它们必须具有动态性,因为存在很多可能性.

but they need to be dynamic since there are many possibilities.

{
"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"
    }
  }
}
}
}

推荐答案

如果您的json字符串键是动态的,则可以将其解析为:

if your json string keys are dynamic then you can parse it as:

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));
          }   
        }
    }
}

这篇关于Android解析JSONObject内部的JSONObjects的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:20