2016-04-23 8 views
0

Это будет просто простой синтаксический анализ JSON, который запутывает меня, поскольку он работает в Javascript, но я не знаю, как это можно реализовать на Java. я получил ответ в конце поста (поставить его там держать вопрос в чистоте) и пытается получить эти значения из этой строки (вы можете найти в ответ):Получение ценности таблицы электронных таблиц Google в Android

«Это первый senstence «. и «Number111»
«Это второе предложение». и "Number222"

Ключи ["gsx$name"]["$t"] и ["gsx$comment"]["$t"]

Я пытаюсь это Java-код, который не вполне есть:

StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
      new Response.Listener<String>() { 
    @Override 
    public void onResponse(String response) { 
     // Display the first 500 characters of the response string. 
     try{ 
      JSONObject array = new JSONObject(response); 
      List<String> list = new ArrayList<String>(); 
      JSONArray array1 = array.getJSONArray("entry.feed"); 
      Log.i("httpget", array1.toString()); 
      //JSONArray array2 = array1.getJSONArray("entry"); 
      for(int i = 0 ; i < array.length() ; i++){ 
       list.add(array1.getJSONObject(i).getString("gsx$name")); 
      } 
      mTextView.setMovementMethod(new ScrollingMovementMethod()); 
      mTextView.setText("Response is: "+array1.toString()); 
     } catch(Exception e){ 
     } 
    } 
} 

Код, который работает в Javascript:

<!DOCTYPE> 
<html> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
<head> 
</head> 
<body> 
<div class="results"></div> 
<script type="text/javascript"> 


// ID of the Google Spreadsheet 
var spreadsheetID = "1Sp18_fOs8SSP_fwB54lv7IoL42uDaioZM1LTRD4rL8o"; 

// Make sure it is public or set to Anyone with link can view 
var url = "https://spreadsheets.google.com/feeds/list/" + spreadsheetID + "/od6/public/values?alt=json"; 

$.getJSON(url, function(data) { 

    var entry = data.feed.entry; 


    $(entry).each(function(){ 
    for (var prop in this["gsx$name"]){alert(this["gsx$name"][prop]);} 
    // Column names are name, age, etc. 
    $('.results').prepend('<h2>'+this.gsx$name.$t+'</h2><p>'+this.gsx$age.$t+'</p>'); 
    }); 

}); 

</script> 
</body> 
</html> 

Ответ от Google Получить звонок:

{"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":{"$t":"https://spreadsheets.google.com/feeds/list/1Sp18_fOs8SSP_fwB54lv7IoL42uDaioZM1LTRD4rL8o/od6/public/values"},"updated":{"$t":"2016-03-08T11:19:18.322Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#list"}],"title":{"type":"text","$t":"Sheet1"},"link":[{"rel":"alternate","type":"application/atom+xml","href":"https://docs.google.com/spreadsheets/d/1Sp18_fOs8SSP_fwB54lv7IoL42uDaioZM1LTRD4rL8o/pubhtml"},{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/1Sp18_fOs8SSP_fwB54lv7IoL42uDaioZM1LTRD4rL8o/od6/public/values"},{"rel":"http://schemas.google.com/g/2005#post","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/1Sp18_fOs8SSP_fwB54lv7IoL42uDaioZM1LTRD4rL8o/od6/public/values"},{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/1Sp18_fOs8SSP_fwB54lv7IoL42uDaioZM1LTRD4rL8o/od6/public/values?alt\u003djson"}],"author":[{"name":{"$t":"amirfarazmand"},"email":{"$t":"[email protected]"}}],"openSearch$totalResults":{"$t":"2"},"openSearch$startIndex":{"$t":"1"},"entry":[{"id":{"$t":"https://spreadsheets.google.com/feeds/list/1Sp18_fOs8SSP_fwB54lv7IoL42uDaioZM1LTRD4rL8o/od6/public/values/cokwr"},"updated":{"$t":"2016-03-08T11:19:18.322Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#list"}],"title":{"type":"text","$t":"This is first senstence."},"content":{"type":"text","$t":"comment: Number111"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/1Sp18_fOs8SSP_fwB54lv7IoL42uDaioZM1LTRD4rL8o/od6/public/values/cokwr"}],"gsx$name":{"$t":"This is first senstence."},"gsx$comment":{"$t":"Number111"}},{"id":{"$t":"https://spreadsheets.google.com/feeds/list/1Sp18_fOs8SSP_fwB54lv7IoL42uDaioZM1LTRD4rL8o/od6/public/values/cpzh4"},"updated":{"$t":"2016-03-08T11:19:18.322Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#list"}],"title":{"type":"text","$t":"This is second sentences."},"content":{"type":"text","$t":"comment: Number222"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/1Sp18_fOs8SSP_fwB54lv7IoL42uDaioZM1LTRD4rL8o/od6/public/values/cpzh4"}],"gsx$name":{"$t":"This is second sentences."},"gsx$comment":{"$t":"Number222"}}]}} 

ответ

1

Этот экстракт кодируют gsx$name и gsx$comment от вашего ответа:

try { 
    JSONObject jsonObject = new JSONObject(response); 
    JSONObject feed = jsonObject.getJSONObject("feed"); 
    JSONArray entryArray = feed.getJSONArray("entry"); 

    for (int i = 0; i < entryArray.length(); i++) { 
     JSONObject entry = entryArray.getJSONObject(i); 

     String name = entry.getJSONObject("gsx$name").getString("$t"); 
     String comment = entry.getJSONObject("gsx$comment").getString("$t"); 

     Log.d(TAG, String.format("%s - %s", name, comment)); 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
Смежные вопросы