2017-03-28 51 views
-3

我是新來的android,因爲我試圖解析Google的電子表格數據,這對我來說有點複雜,我必須獲取$ t的gsx $標記和$ t的gsx $ datetime,如何解析Android中電子表格的複雜JSON數據?

這是從中我們可以得到JSON數據 https://spreadsheets.google.com/feeds/list/1_AR0zX6Jv0NI_R1HBULbPEIUuJ2mqVbLoHVvLqOwu1I/1/public/values?alt=json

數據看起來像這樣

{ 
"version":"1.0", 
"encoding":"UTF-8",  
"feed":{ 
    "xmlns":"http://www.w3.org/2005/Atom", 
    "xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/", 
    "xmlns$gsx":"http://schemas.google.com/spreadsheets/2006/extended", 
    "id":{ }, 

    "updated":{ }, 
    "category":[ ], 
    "title":{ }, 
    "link":[ ], 
    "author":[ ], 
    "openSearch$totalResults":{ }, 
    "openSearch$startIndex":{ }, 
    "entry":[ 
    { 
     "id":{ }, 
     "updated":{ }, 
     "category":[ ], 
     "title":{ }, 
     "content":{ }, 
     "link":[ ], 
     "gsx$id":{ 
      "$t":"1" 
     }, 
     "gsx$datetime":{ 
      "$t":"3/28/2017" 
     }, 
     "gsx$tag":{ 
      "$t":"21" 
     } 
    }, 
    { Data here }, 
    { Data here } 
    ] 
    } 
    } 

和下面的鏈接是我想

HttpHandler sh = new HttpHandler(); 
     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url); 

     Log.e(TAG, "Response from url: " + jsonStr); 
     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 

       JSONObject mainObj = new JSONObject(jsonStr); 
       Log.e(TAG,"JSon Data"+mainObj); 
       if (mainObj != null) { 
        Log.e(TAG,"JSon before feed"+mainObj.toString()); 
        JSONArray a = mainObj.getJSONArray("feed"); 
        Log.e(TAG,"JSon after feed"+a.toString()); 
        JSONObject entru= new JSONObject((Map) a); 
        JSONArray list = entru.getJSONArray("entry"); 
    //      JSONArray entryarray = list.getJSONArray("entry"); 
        if (list != null) { 
         for (int i = 0; i < list.length(); i++) { 
          JSONObject elem = list.getJSONObject(i); 
          if (elem != null) { 
           JSONArray prods = elem.getJSONArray("gsx$tag"); 
           if (prods != null) { 
            for (int j = 0; j < prods.length(); j++) { 

             JSONObject innerElem = prods.getJSONObject(j); 
             if (innerElem != null) { 
              String sku = innerElem.getString("$t"); 
              Log.e(TAG, "$t" + sku); 
              HashMap<String, String> contact = new HashMap<>(); 

              contact.put("$t Datetime", sku); 

              // adding contact to contact list 
              contactList.add(contact); 
             } 
            } 
           } 
          } 
         } 
        } 
       } 
代碼3210

幫我在這方面,謝謝你在前進

+0

檢查我的答案,並嘗試 –

+0

我更新了我的答案,並把完整的代碼。請檢查是否有幫助 –

回答

0
JSONArray a = mainObj.getJSONArray("feed"); 
Log.e(TAG,"JSon after feed"+a.toString()); 
JSONObject entru= new JSONObject((Map) a); 

替換下面一個

JSONObject entru= mainObj.getJSONObject("feed"); 

這3條線與同時更換這條線

JSONArray prods = elem.getJSONArray("gsx$tag"); 

與這一個

JSONObject prods = elem.getJSONObject("gsx$tag"); 

我認爲這是正確的代碼。

HttpHandler sh = new HttpHandler(); 
    // Making a request to url and getting response 
    String jsonStr = sh.makeServiceCall(url); 

    Log.e(TAG, "Response from url: " + jsonStr); 
    if (jsonStr != null) { 
     try { 
      JSONObject jsonObj = new JSONObject(jsonStr); 

      JSONObject mainObj = new JSONObject(jsonStr); 
      Log.e(TAG,"JSon Data"+mainObj); 
      if (mainObj != null) { 
       Log.e(TAG,"JSon before feed"+mainObj.toString()); 
       JSONObject entru= mainObj.getJSONObject("feed"); 
       JSONArray list = entru.getJSONArray("entry"); 
    //JSONArray entryarray = list.getJSONArray("entry"); 
       if (list != null) { 
        for (int i = 0; i < list.length(); i++) { 
         JSONObject elem = list.getJSONObject(i); 
         if (elem != null) { 
          JSONObject prods = elem.getJSONObject("gsx$tag"); 
          if (prods != null) { 
           for (int j = 0; j < prods.length(); j++) { 


             String sku = prods.getString("$t"); 
             Log.e(TAG, "$t" + sku); 
             HashMap<String, String> contact = new HashMap<>(); 

             contact.put("$t Datetime", sku); 

             // adding contact to contact list 
             contactList.add(contact); 

           } 
          } 
         } 
        } 
       } 
      } 

請試試這個。如果這不起作用,請隨時詢問。將幫助你

+0

感謝您的回答,我已接受您的答案作爲最後一個 –

+0

歡迎。高興地幫助你:) :) –