什么';我的Java JSON解析器出了什么问题?

什么';我的Java JSON解析器出了什么问题?,java,json,parsing,Java,Json,Parsing,我对解析JSON非常陌生,我在这里使用解析器。我试图解析来自《纽约时报》的数据,特别是“文档”中的“web_URL”和“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)

我对解析JSON非常陌生,我在这里使用解析器。我试图解析来自《纽约时报》的数据,特别是“文档”中的“web_URL”和“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:

下面是我的代码:

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);  
}
} 
导入java.io.BufferedReader;
导入java.io.DataOutputStream;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.URL;
导入javax.net.ssl.HttpsURLConnection;
导入JSON.JSONObject;
公共类新闻
{
/*实例字段*/
私有最终静态字符串apiNYT=“####”;//NYTimes的api密钥
/**
*这是从中检索文章的HTTP Get请求
*@抛出异常
*@param url请求
*/
私有void sendGet(字符串url)引发异常
{
URL obj=新URL(URL);
HttpURLConnection con=(HttpURLConnection)obj.openConnection();
//阅读JSON
BufferedReader in=新的BufferedReader(新的InputStreamReader(con.getInputStream());
字符串输入线;
//响应是StringBuffer类型的JSON形式
StringBuffer响应=新的StringBuffer();
而((inputLine=in.readLine())!=null){
追加(inputLine);
}
in.close();
//jstr是JSON表单的字符串版本
字符串jstr=response.toString();
//打印结果
系统输出打印LN(jstr);
JSONObject jsonObj=新的JSONObject(jstr);
//arr是记录docs数组的JSONArray
JSON.JSONArray arr=jsonObj.getJSONArray(“文档”);
字符串blurb=“”;
字符串标题=”;
//循环遍历docs数组
对于(int i=0;i
试着这样做:

JSON.JSONArray arr = jsonObj.getJSONObject("response").getJSONArray("docs");
docs
数组嵌套在
response
对象中,因此需要首先处理该级别。您可能希望将其拆分为多行并添加一些空检查。

private void sendGet(字符串url)引发异常
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);

}
{ URL obj=新URL(URL); HttpURLConnection con=(HttpURLConnection)obj.openConnection(); //阅读JSON BufferedReader in=新的BufferedReader(新的InputStreamReader(con.getInputStream()); 字符串输入线; //响应是StringBuffer类型的JSON形式 StringBuffer响应=新的StringBuffer(); 而((inputLine=in.readLine())!=null){ 追加(inputLine); } in.close(); //jstr是JSON表单的字符串版本 字符串jstr=response.toString(); //打印结果 系统输出打印LN(jstr); JSONObject jsonObj=新的JSONObject(jstr); //arr是记录docs数组的JSONArray **JSON.JSONArray arr=jsonObj.getJSONObject(“响应”).getJSONArray(“文档”)** 字符串blurb=“”; //循环遍历docs数组 对于(int i=0;i
“文档”在“响应”中。学习如何阅读JSON。(使用格式化程序进行格式化可能会有所帮助,例如)非常感谢。成功了。但是,我现在正试图找出如何将
main
嵌套在
headline
嵌套在
docs
嵌套在
response
中。当我在for循环中包含以下内容时,为什么会出现
JSONObject[“main”]not found“
错误?
title+=arr.getJSONObject(I).getJSONObject(“headline”).getString(“main”)
@tbee-访问json.org学习json语法。学习只需5-10分钟。并非所有
文档
条目在
标题
对象中都有
main
字符串。例如,关于“绿豆砂锅菜”的第五个条目没有。请使用
optString()
而不是
getString()