这只是一个简单的JSON解析,这对我来说很困惑,因为它在Javascript中工作,但我不知道如何在Java中解决它。 我在帖子的末尾得到了回复(把它放在那里以保持问题清洁)并尝试从该字符串中获取这些值(您可以在响应中找到):
"这是第一次感受。"和" 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":"amirfarazmand@gmail.com"}}],"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"}}]}}
答案 0 :(得分: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();
}