Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

android - how can i return JSONObject data as array

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;

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can post link get json or your json. If can u should try gson library for parse json, ex here: gson example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...