java - Android:使用 Gson 解析 JSON 文件时出错

标签 java android json android-layout gson

我想记录 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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="274a426754484a424b4e494c0944484a" rel="noreferrer noopener nofollow">[email protected]</a>",
                        "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 类

        //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?谢谢

最佳答案

为什么使用该代码来解析 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 无法知道 List<SettingsElement> 的类由于 type erasure .


编辑:我编写的代码对我有用,我的意思是,它解析 JSON 响应并将数据存储在 List<SettingsElement> 中。 ,但请注意,在定义类的方式中,您不会获得 JSON 中的所有数据,我认为这是经过深思熟虑的,实际上简化了一切,因为您的 JSON 很友好很痛苦;)

关于java - Android:使用 Gson 解析 JSON 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16183450/

相关文章:

java - 在 toString 方法中将对象转换为特定数据类型

java - hibernate中的 session 和 session 工厂

java - 从 Android 应用导出文件

PHP/MySQL : SyntaxError: JSON. 解析:JSON 数据第 1 行第 1 列出现意外字符

Ruby - 迭代解析的 JSON

java - logp 不打印

Java Servlet 线程

android - 带有图像和文本以及可检查属性的 ListView

可以补偿倾斜和俯仰的 Android 指南针

java - Spring MVC 可以反序列化 JSON(可以是对象或数组)吗?