سؤال

I'm trying to parse this json:

[
   {
      "game":{
         "id":"203",
         "name":"Blazing Angels 2: Secret Missions of WWII",
         "release_date":"2007-11-06",
         "timeStamp":"2013-06-05 04:12:17",
         "rating":"T",
         "description":"Blazing Angels 2: Secret Missions shows World War II from a different perspective \u00e2\u20ac\u201c that of an elite pilot who is part of a secret team. In the game, the player undertakes some of the most dangerous missions in exotic locations all over the globe. As an elite pilot, the player goes from one great adventure to another, while battling the most lethal experimental weapons of the Third Reich. The squad\\\u0027s mission is to prevent the ultimate weapon from being built. To help players in this quest, they have at their disposal an assortment of highly experimental aircraft and weaponry. Secret Missions comes straight out of the history books of World War II\\\u0027s most top secret projects. Fly above hills and mountains, navigate through treacherous mountain passes and dogfight in the clouds with enemies. Take part in large-scale battles through various conditions: darkness, rain, snow, and air turbulence. Secret Missions takes players around the world, from Paris, Rome and Moscow to exotic locations like Rangoon and Cairo and the highest mountain ranges of the Himalayas. Each mission brings its own surprises, twists and unique challenges. Players have access to some of the most advanced and unusual aircraft and weapons of WWII, as well as access to a huge assortment of weapons, including high-velocity cannons and early guided missiles. Multiplayer features free-for-all, co-op and squad-based play, including the brand new Capture the Flag and Epic Battle modes, the latter pitting two teams of players against each other in a large-scale battle with conquerable bases and objectives that can be destroyed in order to gain the upper hand on the opponent. [Ubisoft]",
         "publisher":{
            "1":"Ubisoft"
         },
         "platform":{
            "2":"ps3",
            "3":"xbox360",
            "4":"pc"
         },
         "genre":{
            "5":"Simulation",
            "6":"Flight",
            "7":"WWII"
         },
         "developer":{
            "8":"\u003Ca href=\u0022\/game\/pc\/blazing-angels-2-secret-missions-of-wwii\u0022 class=\u0022hover_none\u0022\u003EPC\u003C\/a\u003E,\u003Ca href=\u0022\/game\/xbox-360\/blazing-angels-2-secret-missions-of-wwii\u0022 class=\u0022hover_none\u0022\u003EXbox 360\u003C\/a\u003E"
         },
         "game_img":"http:\/\/test-staging.com\/gamer\/gamer_imgs\/203.jpg"
      }
   }]

But when I do start my app, I get this from logCat:

09-19 15:50:08.050: E/AndroidRuntime(2951): FATAL EXCEPTION: AsyncTask #1
09-19 15:50:08.050: E/AndroidRuntime(2951): java.lang.RuntimeException: An error occured while executing doInBackground()
09-19 15:50:08.050: E/AndroidRuntime(2951):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-19 15:50:08.050: E/AndroidRuntime(2951):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-19 15:50:08.050: E/AndroidRuntime(2951):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-19 15:50:08.050: E/AndroidRuntime(2951):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-19 15:50:08.050: E/AndroidRuntime(2951):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-19 15:50:08.050: E/AndroidRuntime(2951):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-19 15:50:08.050: E/AndroidRuntime(2951):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-19 15:50:08.050: E/AndroidRuntime(2951):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-19 15:50:08.050: E/AndroidRuntime(2951):     at java.lang.Thread.run(Thread.java:856)
09-19 15:50:08.050: E/AndroidRuntime(2951): Caused by: java.lang.NullPointerException
09-19 15:50:08.050: E/AndroidRuntime(2951):     at com.myapp.gamers.activities.SplashActivity$1.doInBackground(SplashActivity.java:50)
09-19 15:50:08.050: E/AndroidRuntime(2951):     at com.myapp.gamers.activities.SplashActivity$1.doInBackground(SplashActivity.java:1)
09-19 15:50:08.050: E/AndroidRuntime(2951):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-19 15:50:08.050: E/AndroidRuntime(2951):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-19 15:50:08.050: E/AndroidRuntime(2951):     ... 5 more

And this is my code:

public void loadJSON() throws JSONException {
        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected Void doInBackground(Void... params) {

                JSONObject json = new JSONParser()
                        .getJSONFromUrl(getString(R.string.json_url));

                JSONObject theJSON;
                try {
                    // Getting Array of albums
                    theJSON = json.getJSONObject("game");
                    Log.v("--", theJSON.toString());

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);

            }
        };

        task.execute();
    }

Can anyone tell me where is the problem and how can I parse this Json?

هل كانت مفيدة؟

المحلول

Based on your JSON string the JSONParser() .getJSONFromUrl(getString(R.string.json_url)); should return a JSONArray.

Try to replace your doInBackground method with

 protected Void doInBackground(Void... params) {

            JSONArray json = new JSONParser()
                    .getJSONFromUrl(getString(R.string.json_url));

            JSONObject theJSON;
            try {
                // Getting Array of albums
                theJSON = json.getJSONObject(0).getJSONObject("game");
                Log.v("--", theJSON.toString());

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

نصائح أخرى

Try to remove the [ ] Brackets from your JSOn and do it again. What you have is an Array of "game" objects

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top