【问题标题】:How to parse Complex JSON data of spreadsheet in android?如何在android中解析电子表格的复杂JSON数据?
【发布时间】:2017-03-28 12:16:06
【问题描述】:

我是 android 新手,因为我正在尝试解析 Google 的电子表格数据并且它对我来说有些复杂,所以我必须获取 gsx$tag 的 $t 和 gsx$datetime 的 $t,

这是我们可以从中获取 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);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

在这方面帮助我,提前谢谢你

【问题讨论】:

  • 在下面查看我的答案并尝试
  • 我更新了我的答案并输入了完整的代码。请检查是否有帮助

标签: android json google-sheets


【解决方案1】:
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);

                                }
                            }
                        }
                    }
                }
            }

请试试这个。如果这不起作用,请随时询问。会帮助你

【讨论】:

  • 感谢您的回答,我已接受您的回答作为最后一个
  • 欢迎。很高兴为您提供帮助:) :)
【解决方案2】:

您可以尝试使用GsonJackson

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2011-12-31
    • 1970-01-01
    相关资源
    最近更新 更多