Android Json请教错误

时间:2016-09-09 04:15:03

标签: java android json android-studio

我在JSON数据结构中获取数组时遇到的问题是密钥"标记"但Android Studio给我一个错误,说这个值不存在。

咨询是:http://content.guardianapis.com/search?q=smartphone&page-size=1&api-key=test&order-by=newest&show-tags=contributor

try {
            JSONObject baseJsonResponse = new JSONObject(newsJSON);
            JSONObject response = baseJsonResponse.getJSONObject("response");
            JSONArray results = response.getJSONArray("results");

            for (int i = 0; i < results.length(); i++) {

                JSONObject result = results.getJSONObject(i);
                String section = result.getString("sectionName");
                String title = result.getString("webTitle");
                String date = result.getString("webPublicationDate");
                String url = result.getString("webUrl");

                JSONArray tags = result.getJSONArray("tags");


                newses.add(new News(section, title, date, url));

            }
  

错误&gt;解析新闻JSON结果时出现问题     org.json.JSONException:标签没有值

以下是logcat日志:

Problem parsing the news JSON results
org.json.JSONException: No value for tags
  at org.json.JSONObject.get(JSONObject.java:389)
  at org.json.JSONObject.getJSONArray(JSONObject.java:584)
  at com.luistejada.android.news.utils.Utils.extractFeatureFromJson(Utils.java:159)
  at com.luistejada.android.news.utils.Utils.fetchNewsData(Utils.java:62)
  at com.luistejada.android.news.news.NewsLoader.loadInBackground(NewsLoader.java:42)
  at com.luistejada.android.news.news.NewsLoader.loadInBackground(NewsLoader.java:14)
  at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
  at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
  at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:66)
  at android.os.AsyncTask$2.call(AsyncTask.java:295)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
  at java.lang.Thread.run(Thread.java:818)

3 个答案:

答案 0 :(得分:1)

你的代码对我工作正常.....请检查截图,我能够获得标签值...

enter image description here

values inside tags

答案 1 :(得分:0)

我已尝试过上述内容并获得以下内容

     <div class='alert alert-danger alert-dismissible' role='alert' id=    "landing-page-error">
      <button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
      <span class='glyphicon glyphicon-warning-sign' aria-hidden='true'>  </span> </div>
 <p class = "error-message"> Sign-Up Error </p>
  

输出

public static void main(String[] args) throws IOException, JsonIOException, ParseException {
    JsonObject json = readJsonFromUrl("http://content.guardianapis.com/search?q=smartphone&page-size=1&api-key=test&order-by=newest&show-tags=contributor");
    //System.out.println(json);


    try {

        JsonArray results = json.get("response").getAsJsonObject().get("results").getAsJsonArray();

        for (int i = 0; i < results.size(); i++) {

            JsonObject result = results.get(i).getAsJsonObject();

            JsonArray tags = result.getAsJsonArray("tags");
            System.out.println(tags);

        }
    }catch(Exception e){}
}

考虑将[{"id":"profile/editorial","type":"contributor","webTitle":"Editorial","webUrl":"https://www.theguardian.com/profile/editorial","apiUrl":"https://content.guardianapis.com/profile/editorial","bio":"<p>Editorials from the Guardian. All Guardian and Observer editorials can be found <a href=\"http://www.guardian.co.uk/tone/editorials\">here</a></p>","firstName":"","lastName":"editorial"}] 类与POJO一起使用,以使工作更简单。这是一个将您的JSON结果转换为Gson

的网站
POJO

试一试。

以下是我从api端点提供的http://www.jsonschema2pojo.org 响应

Json

答案 2 :(得分:0)

试试这个,这对你的api来说是完美的工作

url = new URL("http://content.guardianapis.com/search?q=smartphone&page-size=3&api-key=test&order-by=newest&show-tags=contributor");
                HttpURLConnection urlConnection  =(HttpURLConnection) url.openConnection();
                InputStream in = urlConnection.getInputStream();
                InputStreamReader isw = new InputStreamReader(in);
                BufferedReader r = new BufferedReader(isw);
                StringBuilder totalb = new StringBuilder();
                String line="";
                while ((line = r.readLine()) != null) {
                    totalb.append(line).append('\n');
                }
                try {
                    Log.e("tagobj", "line"+totalb.toString());
                    JSONObject obj = new JSONObject(totalb.toString());
                    JSONObject object = obj.getJSONObject("response");
                    String status = object.getString("status"); 

                    JSONArray resultArrat = object.getJSONArray("results");
                    for(int i = 0; i < resultArrat.length(); i++){
                        JSONObject jo_inside = resultArrat.getJSONObject(i);
                        Log.i("tagobj", "jo_inside"+jo_inside.toString());

                        String result_id = jo_inside.getString("id"); 

                        JSONArray tag_array = jo_inside.getJSONArray("tags");
                        for(int j = 0; j < tag_array.length(); j++){
                            JSONObject tagObj = tag_array.getJSONObject(j);
                            String tag_id = tagObj.getString("id"); 

                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }