我是Android的新手,因为我正在尝试解析Google的电子表格数据,这对我来说有些复杂,我必须获取$ t的gsx $ tag和$ t的gsx $ datetime,< / p>
这是我们可以从中获取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);
}
}
}
}
}
}
}
在这方面帮助我,提前谢谢
答案 0 :(得分:0)
答案 1 :(得分:0)
JSONArray a = mainObj.getJSONArray("feed");
Log.e(TAG,"JSon after feed"+a.toString());
JSONObject entru= new JSONObject((Map) a);
用下面的一行替换这3行
JSONObject entru= mainObj.getJSONObject("feed");
也替换此行
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);
}
}
}
}
}
}
请试试这个。如果这不起作用,请随时问。会帮助你