2013-09-19 3 views
-1

Я пытаюсь разобрать этот JSON:Ошибка синтаксического анализа 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" 
     } 
    }] 

Но когда я начать мое приложение, я получаю от этого 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 

И это мой код:

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(); 
    } 

Может ли кто-нибудь сказать мне, где проблема, и как я могу разобрать этот Json?

+0

что у вас есть массив JSON '[' represetns узел JSON массива. Строка 50 splashactivity.java? – Raghunandan

+0

@ Raghunandan, так что это значит, как я могу это исправить? –

+0

JSONObject json = new JSONParser() .. shgould будет массивом json. и что такое линия 50 для всплеска активности – Raghunandan

ответ

1

На основе вашей строки JSON JSONParser() .getJSONFromUrl (getString (R.string.json_url)); должен вернуть JSONArray.

Попробуйте заменить метод doInBackground с

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; 
     } 
0

Попробуйте удалить [] кронштейны из вашего JSOn и сделайте это снова. У вас есть массив из «игровых» объектов

+0

игра не является массивом его jsonbject '' game ": {' – Raghunandan

+0

На самом деле у него есть массив объекта с игровым свойством, которое в свою очередь является объектом. –

+0

Yap спасибо за ответ –

Смежные вопросы