java - 我的 Java JSON 解析器出了什么问题?

标签 java json parsing

我对解析 JSON 非常陌生,我正在使用这里的解析器 https://github.com/douglascrockford/JSON-java/ 。我正在尝试解析来自《纽约时报》的数据,特别是“docs”中的“web_urls”和“main”,但出现此错误:

Exception in thread "main" JSON.JSONException: JSONObject["docs"] not found.
at JSON.JSONObject.get(JSONObject.java:475)
at JSON.JSONObject.getJSONArray(JSONObject.java:557)
at News.sendGet(News.java:53)
at News.main(News.java:80)

Exception in thread "main" JSON.JSONException: JSONObject["main"] not found.
at JSON.JSONObject.get(JSONObject.java:475)
at JSON.JSONObject.getString(JSONObject.java:656)
at News.sendGet(News.java:56)
at News.main(News.java:78)

这是 JSON 的 URL:http://api.nytimes.com/svc/search/v2/articlesearch.json?q=water&begin_date=20141101&end_date=20141101&api-key=sample-key

下面是我的代码:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import JSON.JSONObject;


public class News 
{
/* Instance Field */
private final static String apiNYT = "###"; // api key for NYTimes

/**
 * This is an HTTP Get Request to retrieve articles from 
 * @throws Exception
 * @param url request
 */
private void sendGet(String url) throws Exception 
{
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // Read the JSON
    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;

    // Response is the JSON form of type StringBuffer 
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    // jstr is the string version of the JSON form
    String jstr = response.toString();


    //print result
    System.out.println(jstr);

    JSONObject jsonObj = new JSONObject(jstr);

    // arr is a JSONArray that records the docs array
    JSON.JSONArray arr = jsonObj.getJSONArray("docs");
    String blurb = "";
    String title = "";

    // Loop through the docs array
    for (int i = 0; i < arr.length(); i++)
    {
         blurb += arr.getJSONObject(i).getString("web_url");
         title += arr.getJSONObject(i).getJSONObject("headline").getString("main");
    }

    System.out.println(blurb);
    System.out.println(title);
}

/**
 * Main method
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception 
{

    News http = new News();

    // Test call for NYTimes API (articles with the word "water" in title on 14/11/01)
    String url = "http://api.nytimes.com/svc/search/v2/articlesearch.json?q=water&begin_date=20141101&end_date=20141101&api-key="
            + apiNYT ;

    http.sendGet(url);  
}
} 

最佳答案

尝试这样:

JSON.JSONArray arr = jsonObj.getJSONObject("response").getJSONArray("docs");

docs数组嵌套在 response 中对象,因此需要首先处理该级别。您可能希望将其分成多行并添加一些空检查。

关于java - 我的 Java JSON 解析器出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26965002/

相关文章:

java - 如何动态处理facebook API版本升级异常

json - 微软学术API,知识图谱搜索——检索到的论文为空

python - 使用 Flask 将 JSON 数据从服务器传递到客户端

ruby - 警告解析器/当前正在加载解析器/ruby22

php - 用 PHP 编写的语言解析器库

java - 如何在java中通过socket发送Image数据类型

java - 访问 JFrame 组件

java - Jackson 中的序列化数组类型和数组

javascript - 从数组中删除空字符串(正则表达式) - Javascript

java - 并行化异构任务的局限性