我的Java JSON Parser出了什么问题?

时间:2014-11-17 03:21:28

标签: java json parsing

我是解析JSON的新手,我正在使用此处的解析器https://github.com/douglascrockford/JSON-java/。我正在尝试解析来自NYTimes的数据,特别是“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的网址: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);  
}
} 

2 个答案:

答案 0 :(得分:1)

试试这样:

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

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

答案 1 :(得分:1)

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.getJSONObject("response").getJSONArray("docs")**
    String blurb = "";

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

    System.out.println(blurb);

}