我想记录JSON文件中的每个值,但我得到com.example.parsingapi.JsonParse$SettingElement@40d116c98
我在做什么不对吗?
JSON:config.json
[
{
"tbico": "166-newspaper.png",
"t": "",
"sm": [
{
"bgFile": "AppsUn_Bg.png",
"icoSz": "icoSzM",
"sm": [
{
"st": "f",
"t": "Why ",
"f": "Why.pdf",
"cusico": "Button3D.png"
},
{
"st": "f",
"t": "Why",
"tbico": "112-group.png",
"f": "Whysome.pdf",
"cusico": "Why3D.png"
},
{
"st": "f",
"t": "Why Now?",
"f": "Why.pdf",
"cusico": "Button3D.png"
},
{
"t": "",
"st": "blnk"
},
{
"bgFile": "background.png",
"icoSz": "icoSzM",
"sm": [
{
"st": "f",
"t": "Animal Welfare League",
"f": "Page.pdf",
"cusico": "Button.png"
},
{
"st": "f",
"t": "Bada Bing",
"f": "Bing.pdf",
"cusico": "Button.png"
},
{
"st": "f",
"t": "South",
"f": "Page.pdf",
"cusico": "Button.png"
},
{
"st": "f",
"t": "Actor",
"f": "Page.pdf",
"cusico": "U_Button.png"
},
{
"st": "f",
"t": "Trust",
"f": "Kids.pdf",
"cusico": "Button.png"
},
{
"st": "f",
"t": "ELTEC",
"f": "ELTEC.pdf",
"cusico": "Button.png"
}
],
"t": "Examples",
"gvNR": 4,
"gvHIT": true,
"gvNC": 2,
"st": "igsm",
"cusico": "Examples-button3D.png"
},
{
"t": "",
"st": "blnk"
},
{
"bgFile": "Default-Portrait~ipad.png",
"icoSz": "icoSzL",
"sm": [
{
"fb": "somelink",
"t": "Facebook",
"wTBL": "wTBLNB",
"st": "fb",
"cusico": "tab-b-Facebook.png"
},
{
"t": "Tell A Friend",
"st": "shrMnuBtn",
"cusico": "share-icon.png"
},
{
"st": "blnk"
},
{
"st": "blnk"
},
{
"t": "Email Us",
"sndEmlBtn": "me@somelink.com",
"st": "sndEmlBtn",
"cusico": "tab-b-mail.png"
},
{
"wTBL": "wTBLNB",
"t": "Our website",
"w": "http://somelink.com",
"st": "w",
"cusico": "icon-144x144.png"
}
],
"t": "Contact Us",
"gvNR": 3,
"gvHIT": false,
"gvNC": 2,
"st": "igsm",
"cusico": "Contact_Us_Button.png"
},
{
"t": "Login",
"st": "selApp",
"cusico": "Login_Button.png"
}
],
"t": "Home",
"tbico": "53-house.png",
"gvNR": 4,
"gvHIT": true,
"gvNC": 2,
"st": "igsm"
},
{
"bgFile": "background.png",
"icoSz": "icoSzM",
"sm": [
{
"wTBL": "wTBLNB",
"t": "My Invoices",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "My_Invoices_Button.png"
},
{
"wTBL": "wTBLNB",
"t": "My Emails",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "My_Emails_Button.png"
},
{
"wTBL": "wTBLNB",
"t": "My Apps",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "Button.png"
},
{
"wTBL": "wTBLNB",
"t": "My Details",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "my_details_button.png"
},
{
"wTBL": "wTBLNB",
"t": "Change Password",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "Change_Password.png"
},
{
"wTBL": "wTBLNB",
"t": "Order",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "Button.png"
},
{
"wTBL": "wTBLNB",
"t": "Submit Ticket",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "Button_CP.png"
},
{
"wTBL": "wTBLNB",
"t": "Support Tickets",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "Support-Ticket_button_DP.png"
},
{
"wTBL": "wTBLNB",
"t": "Network Issues",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "Network_Issues_Button.png"
}
],
"t": "Client Portal",
"tbico": "b40-dialpad.png",
"gvNR": 3,
"gvHIT": true,
"gvNC": 3,
"st": "igsm"
},
{
"bgFile": "background.png",
"icoSz": "icoSzM",
"sm": [
{
"wTBL": "wTBLNB",
"t": "Pre-Sales Enquires",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "Button.png"
},
{
"st": "blnk"
},
{
"wTBL": "wTBLNB",
"t": "Knowledgebase",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "Button_Knowledgebase.png"
},
{
"wTBL": "wTBLNB",
"t": "Support Forum",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "Button.png"
},
{
"wTBL": "wTBLNB",
"t": "Submit New Ticket",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "Button.png"
},
{
"wTBL": "wTBLNB",
"t": "Support Tickets",
"w": "https://www.somelink.com",
"st": "w",
"cusico": "Support_Tickets.png"
}
],
"t": "Support",
"tbico": "08-chat.png",
"gvNR": 3,
"gvHIT": true,
"gvNC": 2,
"st": "igsm"
}
],
"st": "tbm"
}
]
JSONParse Class
//Settings header class
public class SettingsElement{
//First Objects
String tbico = null;
String t = null;
//second arrays object of sm[]
ArrayList<TableViewMenuSM> sm = null;
//initzialize first object
public String getTbico(){
return tbico;
}
public void setTbico(String tbico){
this.tbico = tbico;
}
public String geT(){
return t;
}
public void setT(String t){
this.t = t;
}
//initzialize and calling array object of sm[0] AS A APP SCREEN LAYOUT
public ArrayList<TableViewMenuSM>getTableViewDetails(){
return sm;
}
public void setTableViewDetails(ArrayList <TableViewMenuSM> sm){
this.sm = sm;
}
}
//Settings screen type class
public class TableViewMenuSM{
String bgFile = null;
String icoSz = null;
String st = null;
//Sub menu sm element
ArrayList <SubMenuScreenType> sm = null;
//Init the first object
public String getBgFile(){
return bgFile;
}
public void setBgFile(String bgFile){
this.bgFile = bgFile;
}
public String getIcoSz(){
return icoSz;
}
public void setIcoSz(String icoSz){
this.icoSz = icoSz;
}
public String setSt(){
return st;
}
public void getSt(String st){
this.st = st;
}
//initzialize and calling array object of sm[1] AS A INSIDE SCREEN ELEMENT
public ArrayList<SubMenuScreenType>getSubMenuScreenType(){
return sm;
}
public void setSubMenuScreenTypes(ArrayList <SubMenuScreenType> sm){
this.sm = sm;
}
}
//Settingup inside element of screen type
public class SubMenuScreenType{
String st = null;
String t = null;
String f = null;
String cusico = null;
String bgFile = null;
String icoSzM = null;
int gvNR;
boolean gvHIT;
int gvNC;
String fb = null;
String wTBL = null;
String sndEmlBtn = null;
String w = null;
//Init the first object
public String getSt(){
return st;
}
public void setSt(String st){
this.st = st;
}
public String getT(){
return t;
}
public void setT(String t){
this.t = t;
}
public String getF(){
return f;
}
public void setF(String f){
this.f = f;
}
public String getCusico(){
return cusico;
}
public void setCusico(String cusico){
this.cusico = cusico;
}
public String getBgFile(){
return bgFile;
}
public void setBgFile(String bgFile){
this.bgFile = bgFile;
}
public String getIcoSzm(){
return icoSzM;
}
public void setIcoSzm(String icoSzM){
this.icoSzM = icoSzM;
}
public int getGvnr(){
return gvNR;
}
public void setGvnr(int gvNR){
this.gvNR = gvNR;
}
public boolean getGvhit(){
return gvHIT;
}
public void setGvhit(boolean gvHIT){
this.gvHIT = gvHIT;
}
public int getGvnc(){
return gvNC;
}
public void setGvnc(int gvNC){
this.gvNC = gvNC;
}
public String getFb(){
return fb;
}
public void setFb(String fb){
this.fb = fb;
}
public String getWtbl(){
return wTBL;
}
public void setWtbl(String wTBL){
this.wTBL = wTBL;
}
public String getSendEmailBtn(){
return sndEmlBtn;
}
public void setSendEmailBtn(String sndEmlBtn){
this.sndEmlBtn = sndEmlBtn;
}
public String getW(){
return w;
}
public void setW(String w){
this.w = w;
}
}
Main.java :(调用函数)
Thread dx = new Thread() {
public void run() {
try {
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()){
Log.i("INWHILE LOOP FROM MAIN", "WHILE STARTS");
JsonElement json2 = (JsonElement) iterator.next();
Gson gson = new Gson();
SettingsElement sett = gson.fromJson(json2, SettingsElement.class);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
dx.start();
我如何正确解析这个config.json?感谢
答案 0 :(得分:2)
为什么使用该代码来解析JSON,而不是像在this other question那样使用通常的方式?
使用你拥有的类,我这样做是为了解析JSON:
Gson gson = new Gson();
Type settingsElementList Type = new TypeToken<List<SettingsElement>>() {}.getType();
List<SettingsElement> settingsElementList = gson.fromJson(jsonString, listSettingsElementType);
请注意,您无法直接使用SettingsElement
类来解析JSON,因为您的JSON以这些对象的数组开头。你不能这样做:
List<SettingsElement> settingsElementList = gson.fromJson(jsonString, List<SettingsElement>.class); //wrong!
之前的代码失败,因为Java因type erasure而无法知道List<SettingsElement>
的类。
编辑:我编写的代码对我有用,我的意思是,它解析JSON响应并将数据存储在List<SettingsElement>
中,但请注意,就像你一样已经定义了你的类,你没有获得 ALL 你JSON中的数据,我认为这是经过深思熟虑的,事实上简化了所有内容,因为你的JSON是一种痛苦的屁股;)
答案 1 :(得分:0)
尝试:[不包括Getter和setter]
public class TableViewMenuSM{
String bgFile = null;
String icoSz = null;
String st = null;
String t = null;
String f = null;
String cusico = null;
String bgFile = null;
String icoSzM = null;
int gvNR;
boolean gvHIT;
int gvNC;
String fb = null;
String wTBL = null;
String sndEmlBtn = null;
String w = null;
ArrayList <TableViewMenuSM> sm = null;
}
不需要SubMenuScreenType。