在Android App中使用Gson反序列化JSON(错误:NullPointer)

时间:2013-04-19 05:08:15

标签: java android performance gson json

在我的Android应用程序中,我正在从服务器获得Json响应:

[
   {
    "sm": [
      {
        "tbico": "b43-jeep.png",
        "t": "Welcome!",
        "w": "http://google.com/start",
        "st": "w",
        "wTBL": "wTBLN"
      }, {
        "t": "Content screen title",
        "f": "Eltec%20Spec%20sheet%20Gim%20RD30W.pdf",
        "st": "f"
      }, {
        "tbico": "109-chicken.png",
        "t": "Sub menu",
        "sm": [
          {
            "t": "Screen 1",
            "st": "f"
          }, {
            "t": "Screen 2",
            "w": "Http://google.com",
            "st": "w",
            "wTBL": "wTBLT"
          }
        ],
        "st": "sm"
      }, {
        "st": "f"
      }
    ],
    "st": "tbm"
  }
]

现在我使用Gson解析这个JSON文件,如:

//USING GSON In Parse.java
public class Parse {
    public class Setting {
        String sett;
        int[] clorBG;
        int[] colrTR;
        int[] colrTRT;
        int fntIcoGrd;
        boolean statBr;
        //List<Share> shrMnuPrest;   //Place name that matches to json from json-file element
        List<SM> sm;
        List<Share> shrMnuPrest;
        /*
        public boolean getStatbar(){
            return statBr;
        }
        public boolean setStatbar(){
            return this.statBr;
        }*/
    }
    public class Share {
        String shrFBLnk;
        String shrTwtBdy;
        String shrTxtMsgBdy;
        String shrFBBdy;
        String sndEmlBtn;
    }
    public class Screen {
        String tbico;
        String t;
        String st;
        List<SM> sm;
        //List<SM2> sm2; should auto load from <SM> List
    }
    public class SM { //for Table View
        String bgFile;
        String icoSz;
        String t;
        boolean gvHIT;
        int gvNR;
        int gvNC;
        String st;
        List<SM2> sm2;
        List<Share> share;
    }
    static class SM2 { //Sub-menu screen type
        String st;
        String t;
        String f;
        String cusico;
        String bgFile;
        String icoSz;
        String fb;
        String wTBL;
        String w;
    }
    public static void main(String[] args) throws Exception {
        Log.i("RUNNING", "Running Main call");
        String jsonLink = readUrl("http://10.0.2.2/" + "json/config.json");
        JsonElement json = new JsonParser().parse(jsonLink);
        JsonArray array = json.getAsJsonArray();
        Iterator iterator = array.iterator();
        while (iterator.hasNext()) {
            JsonElement json2 = (JsonElement) iterator.next();
            Gson gson = new Gson();
            Setting sett = gson.fromJson(json2, Setting.class);
            //prints entire class
            System.out.println(sett.toString());
            //print WHAT you want!!
            System.out.println(sett.statBr);
            System.out.println(sett.clorBG);
            //System.out.println(sett.colrTRT);
            System.out.println(sett.colrTRT);
        }
        //Gson gson = new Gson();
        //Setting sett = gson.fromJson(json, Setting.class);
        //System.out.println(sett.toString());
    }
}

现在当我运行函数时,我在gson中得到NullPointer的错误;所有我想要的是解析这个JSON,以便我可以在我的应用程序中使用此文件的值。(我可以更改JSON文件,因为我没有主服务器上的控件,新的android和JSON感谢帮助。)我在这里发布了代码同样http://pastebin.com/TJMuG67b 错误:http://cl.ly/image/2q0Z0Q0m3F26

0 个答案:

没有答案