我如何返回JSONObject数据作为数组 [英] how can i return JSONObject data as array

查看:118
本文介绍了我如何返回JSONObject数据作为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JSONObject作为数组返回数据,以便我可以遍历它们。在udacity之后进行了android开发培训,但是培训中使用的JSON结构与我使用的JSON结构有所不同。下面是我的课程

I am trying to return the data from a JSONObject as array so i can loop through them. followed android development training on udacity but the JSON structure used in the training is different from what i am using. below is my class

  private String[] getWeatherDataFromJson(String forecastJsonStr)
            throws JSONException {

        // These are the names of the JSON objects that need to be extracted.
        final String OWM_LIST = "rules";
        final String OWM_DESCRIPTION = "game_rules_content";

        JSONObject forecastJson = new JSONObject(forecastJsonStr);
        JSONArray weatherArray = forecastJson.getJSONArray(OWM_LIST);
        Log.v(LOG_TAG, "success: " + forecastJson.getInt("success"));

       //I guess my problem is starts here
       String[] resultStrs = new String[1];

        for(int i = 0; i < weatherArray.length(); i++) {

            String rule;
            // Get the JSON object representing the day
            JSONObject dayForecast = weatherArray.getJSONObject(i);
            rule = dayForecast.getString(OWM_DESCRIPTION);
            Log.v(LOG_TAG, "sammy: " + rule);

             // how do i return my json data as array for i loop through the array with for
            resultStrs[0] = rule;

        }

        for (String s : resultStrs) {
            Log.v(LOG_TAG, "Forecast entry: " + s);
        }
        return resultStrs;

    }


推荐答案

您应该

 resultStrs[i] = rule;

这将解决您的问题。
喝彩。

This will resolve your issue. Cheers.

这篇关于我如何返回JSONObject数据作为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆