开发者

Android update SQLite with GSON response ... how should I parse nested JSON?

I have tried to figure this out on my own... starting with two excellent tutorials on GSON parsing from High-Speed Blog and Java Code Geeks. Several [answers] (http://stackoverflow.com/questions/3763937/gson-and-deserializing-an-array-of-objects-with-arrays-in-it/6300251#6300251) on stackoverflow are relevant, but I need more help than a screendump. I want to update my SQLite database from the following JSON response:

            {
                "Field": "13",
                "Plants": [
                    {
                        "PlantID": 123,
                        "PlotID": 321,
                        "Row": 1,
                        "Post": 1,
                        "Position": 1,
                        "PlotName": "Sunnyside",
                        "Breed": "Daisy"
                    },
                    {
                        "PlantID": 149,
                        "PlotID": 348,
                        "Row": 1,
                        "Post": 20,
                        "Position": 1,
                        "PlotName": "Waterside",
                        "Breed": "Iris"
                    }
                ]
            }

my root class (jsonPlots) and 'nested' class (Plants) are as follows:

    public class jsonPlots {
      @SerializedName("Field")
        public String mFieldId;
      public ArrayList<Plants> PlantArray;
    }
    public class Plants {
        @SerializedName("PlantID")
        public int jPlantID;

        @SerializedName("PlotID")
        public int jPlotID;

        @SerializedName("Row")
        public int jRow; 

        @SerializedName("Post")
        public int jPost;

        @SerializedName("Position")
        public int jPosition;

        @SerializedName("PlotName")
        public String jPlotName;

        @SerializedName("Breed")
        public String jBreed;

    }

I have verified that I am getting a valid response using the following snippet:

    public void getJsonData() {
      InputStream source = retrieveStream(url);     
      Gson gson = new Gson();      
      Reader reader = new InputStreamReader(source);      
      jsonPlots response = gson.fromJson(reader, jsonPlots.class);
      Toast.makeText(this, response.mFieldId, Toast.LENGTH_SHORT).show();  // this works
    ArrayList<Plants> results = response.PlantArray;

    if(results.isEmpty()) { ///  开发者_JS百科  null pointer exception here
      Toast.makeText(this, "no data to update", Toast.LENGTH_SHORT).show();
        }
    else {
        for (Plants result : results) {
              try {
                  Toast.makeText(this, result.jBreed, Toast.LENGTH_SHORT).show();  //  replace with SQLite update
                  Toast.makeText(this, result.jPlotName, Toast.LENGTH_SHORT).show();
                }
              finally {
                  Toast.makeText(this, "failed", Toast.LENGTH_SHORT).show();
                }
        }
    }
}  // getJsonData

The first Toast (mFieldID) works. It crashes at if(results.isEmpty()) with a null pointer exception as noted. Is my ArrayList not populating ? I would like a code suggestion on how to sequence through this nested array... so that I can systematically update my SQLite database.


Gson doesn't know that you want to bind the jsonPlots.PlantArray attribute to the "Plants" JSON element, because the names don't match and there isn't any configuration information (e.g., @SerializedName("Plants")) to otherwise let Gson know to bind them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜