在我的 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