0

IN my android application i'm having Json response from server:

[
   {
    "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"
  }
]

Now Im using Gson to parse this JSON file like:

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

Now when I run function I get error as a NullPointer in gson; All i want is Parse this JSON so that I can use values of this file in my app.(I CANT change JSON file since i dont have control on main server, new to android and JSON thanks for help.) I have posted code here as well http://pastebin.com/TJMuG67b Error : http://cl.ly/image/2q0Z0Q0m3F26

Code Tree
  • 1
  • 1
  • Post your stack trace ? – Triode Apr 19 '13 at 05:14
  • is this out of the question? `JSONObject obj = new JSONObject(JSON STRING);` – string.Empty Apr 19 '13 at 05:18
  • @RajeshCP here is a screenshot: http://cl.ly/image/2q0Z0Q0m3F26 Thanks. – Code Tree Apr 19 '13 at 05:21
  • @NicolasTyler YES, Its already in main function above, Thanks. – Code Tree Apr 19 '13 at 05:24
  • Your mappings are way off. See [here](http://stackoverflow.com/questions/16019117/java-json-program-not-showing-any-output) for a description of how to map a JSON data structure to a Java class. – Perception Apr 19 '13 at 05:34
  • Do you have `glHdr` in your `Setting` class ? – AllTooSir Apr 19 '13 at 05:35
  • There seems to be a problem with your JSON use http://jsonviewer.stack.hu/ to check your JSON. – string.Empty Apr 19 '13 at 05:39
  • @NicolasTyler I have validate JSON, No error found. – Code Tree Apr 19 '13 at 05:52
  • Your JSON reponse is quite complex and your Java classes don't map the JSON reponse, so GSON can't parse it... Do you need **ALL** the data in the JSON reponse or only some fields? – MikO Apr 19 '13 at 10:08
  • @MikO Hi Miko; No, i dont need all the values from json, I have updated JSON to the values that i need, please have a look and advice me any possible solution to JSON. – Code Tree Apr 20 '13 at 09:28
  • Don't modify the JSON you are receiving, because otherwise we can't help properly... Please provide the JSON exactly as you are receiving it, and **bold** the parts you need to retrieve... or just include 2 pieces of JSON code: one with the actual response, and other with the data you need... – MikO Apr 20 '13 at 10:38
  • @MikO Hey Miko visit http://pastebin.com/TJMuG67b for full json, I dont need Object at index 0 so we can avoid that for now all, i need is value of object at index 1 – DPP Apr 22 '13 at 00:46

0 Answers0