【问题标题】:Error parsing json android解析json android时出错
【发布时间】:2013-09-19 13:52:10
【问题描述】:

我正在尝试解析这个 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?

【问题讨论】:

  • 你有一个json数组[代表一个json数组节点。第 50 行 splashactivity.java?
  • @Raghunandan 那是什么意思,我该如何解决?
  • JSONObject json = new JSONParser().. 应该是一个 json 数组。飞溅活动的第 50 行是什么

标签: android json android-parser


【解决方案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;
        }

【讨论】:

    【解决方案2】:

    尝试从您的 JSOn 中删除 [ ] 括号并再次执行此操作。你所拥有的是一个“游戏”对象数组

    【讨论】:

    • 游戏不是数组,它的 jsonbject "game":{
    • 实际上他有一个具有游戏属性的对象数组,而该对象又是一个对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多