Android - Java如何循环多个数组

时间:2017-01-25 14:21:20

标签: java php android loops

ANDROID BEGINS

String JsonFeeds = object.getString("feeds");
JSONArray feeds = new JSONArray(JsonFeeds);
int feed_id;
String feed_radio, feed_title, feed_api, feed_frequency, feed_type, feed_date;


    for (int i=0; i < feeds.length();  i++){
       String feed_data = feeds.getString(i);
       JSONArray array_feed = new JSONArray(feed_data);
       for (int j=0; j < array_feed.length();  j++) {
          JSONObject row = array_feed.getJSONObject(j);
          feed_title = row.getString("feed_title");
          tv.setText(feed_title);
       }
    }

我只是android中的初学者,我试图找出如何进行跟随循环

预期循环,如果它是PHP

foreach($array[feeds] as $feed_key => $array_feed_data){
    echo "<div>";
        echo "<span>" . $array[feeds][$feed_key][val1] . "<span>";
        echo "<span>" . $array[feeds][$feed_key][val2] . "<span>";
        echo "<span>" . $array[feeds][$feed_key][val3] . "<span>";
        echo "<span> <a href='$downloadlink'> Download </a><span>";
    echo "</div>";  
}

我在使用Android时遇到的问题,我无法访问feeds[array_feed_id][my_value_to_show]

修改

 String JsonFeeds = object.getString("feeds");
 JSONArray feeds = new JSONArray(JsonFeeds);

JSONObject feeds = object.getJSONArray("feeds");

                   for (String key : feeds.getKeys()) {
                       JSONObject row = feeds.getJSONObject(key);
                       feed_title = row.getString("feed_title");
                       tv.setText(feed_title);
                   }

1 个答案:

答案 0 :(得分:0)

feed本身就是一个JSONObject,而不是JSONArray,因为它的元素被命名。试一试:

JSONObject feeds = objects.getJSONObject("feeds");

Iterator<String> keys = feeds.keys();
while (keys.hasNext()) {
    JSONObject row = feeds.getJSONObject(keys.next());
    feed_title = row.getString("feed_title");
    tv.setText(feed_title);
}