Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have been following a tutorial and finally have understand to a decent degree using AsyncTask and how to send an http get request to get the json returned. I can get the json, I think successfully but am having trouble parsing it.

The tutorial I was looking at uses a pretty simple weather api which sends back pretty easy json to parse.

Mine is a search result with info on each item. My json looks like this:

http://pastebin.com/f65hNx0z

I realize the difference between json objects and the arrays of info. Just a bit confused on how to parse over to get information on each beer and the brewery info.

My code below:

String jsonUrl = url + query;
            Toast.makeText(this, jsonUrl, Toast.LENGTH_SHORT).show();

            //todo: get json 
            new ReadJSONResult().execute(jsonUrl);

            return false;
        }

        private class ReadJSONResult extends AsyncTask
        <String, Void, String> {
            protected String doInBackground(String... urls) {
                return readJSONFeed(urls[0]);
            }

            protected void onPostExecute(String result) {
                try {

                    ///code below is what I kow I need to reconstruct and change to parse
                    JSONObject jsonObject = new JSONObject(result);
                    JSONObject weatherObservationItems = 
                        new JSONObject(jsonObject.getString("weatherObservation"));

                    Toast.makeText(getBaseContext(), 
                        weatherObservationItems.getString("clouds") + 
                     " - " + weatherObservationItems.getString("stationName"), 
                     Toast.LENGTH_SHORT).show();


                } catch (Exception e) {
                    Log.d("ReadWeatherJSONFeedTask", e.getLocalizedMessage());
                }          
            }
        }
See Question&Answers more detail:os

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

1 Answer

You should use a JSON deserializer library to object which supports nested objects, also. I recommend Gson https://code.google.com/p/google-gson/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...