2013-04-24 3 views
2

JSON 파일의 모든 값을 기록하려고합니다. 모두 얻을 수 있습니다. [email protected] 내가 뭘하고 있는거야?안드로이드 : Gson을 사용하여 JSON 파일을 구문 분석하는 동안 오류가 발생했습니다.

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": "[email protected]", 
                        "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 올바른 방법을 분석 할 ? 감사합니다

답변

2

this other question에서와 마찬가지로 평범한 방식을 사용하는 대신 JSON을 구문 분석하는 데 코드를 사용하는 이유는 무엇입니까? 당신의 JSON 배열로 시작하기 때문에 당신이 JSON을 구문 분석 직접 SettingsElement 클래스를 사용할 수 없습니다

Gson gson = new Gson(); 
Type settingsElementList Type = new TypeToken<List<SettingsElement>>() {}.getType(); 
List<SettingsElement> settingsElementList = gson.fromJson(jsonString, listSettingsElementType); 

참고 : 당신이 가지고있는 클래스를 사용

, 나는 JSON을 구문 분석이 작업을 수행 할 것 그 물체들. 그리고 당신도 이런 식으로 수행 할 수 없습니다 자바 type erasure에 의한 List<SettingsElement>의 클래스를 알 수 없기 때문에

List<SettingsElement> settingsElementList = gson.fromJson(jsonString, List<SettingsElement>.class); //wrong! 

앞의 코드는 실패합니다.


편집 : 나는 나를 위해 노력하고 작성한 코드는, 내 말은, 그것은 JSON 응답을 구문 분석하고 List<SettingsElement>에 데이터를 저장하지만, 당신이 당신의 클래스를 정의한 방식으로 점에 유의 , 당신은 얻고 있지 않다 ALL 당신의 JSON에있는 데이터, 나는 이것이 숙고 된 것 같아요, 당신의 JSON이 엉덩이에 고통의 종류이기 때문에 실제로 모든 것을 단순화합니다;)

+0

안녕하세요 Miko, 다른 코드가 작동하지만 위의 json 파일에서 35 번째 줄에있는 Object를 가져올 수 없습니다. (어떤 생각?이 수준의 복잡성을 JSON으로 구문 분석하려면 다른 방법을 사용해야한다고 생각하십니까? – star18bit

0

시도가 : [게터와 세터가 포함되어 있지 않습니다]

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이 필요하지 않습니다.

+0

Na 이런 식으로 NullPointer 오류가 발생합니다 다른 해킹? – star18bit

+0

"기록"하는 코드는 어디에 있습니까? –

관련 문제