Java 试图从JSON中提取,但它';s空

Java 试图从JSON中提取,但它';s空,java,arrays,json,parsing,Java,Arrays,Json,Parsing,我会在这里解释我的问题。 我有一个程序,该程序假定通过从网络爬虫接收的传入JSON文件进行解析 public static void Scan(Article article) throws Exception { //When running program, creates a error text-file inside java Project folder File file = new File("errorlogg.txt"); FileWriter

我会在这里解释我的问题。 我有一个程序,该程序假定通过从网络爬虫接收的传入JSON文件进行解析

    public static void Scan(Article article) throws Exception
{
    //When running program, creates a error text-file inside java Project folder
    File file = new File("errorlogg.txt");
    FileWriter fileWriter = new FileWriter(file, true);

    // if file doesn't exists, then create it
    if (!file.exists()) 
    {
        file.createNewFile();
    }

    //Setting up an URL HttpURLConnection given DOI
    URL urlDoi = new URL (article.GetElectronicEdition());

    //Used for debugging 
    System.out.println("Initial DOI: " + urlDoi);

    //Transform from URL to String
    String doiCheck = urlDoi.toString();

    //Redirect from IEEE Xplore toe IEEE Computer Society
    if(doiCheck.startsWith("http://dx."))
    {
        doiCheck = doiCheck.replace("http://dx.doi.org/", "http://doi.ieeecomputersociety.org/");
        urlDoi = new URL(doiCheck);
    }

    HttpURLConnection connDoi = (HttpURLConnection) urlDoi.openConnection();

    // Make the logic below easier to detect redirections
    connDoi.setInstanceFollowRedirects(false);  

    String doi = "{\"url\":\"" + connDoi.getHeaderField("Location") + "\",\"sessionid\":\"abc123\"}";

    //Setting up an URL to translation-server
    URL url = new URL("http://127.0.0.1:1969/web");
    URLConnection conn = url.openConnection();

    conn.setDoOutput(true);
    conn.setRequestProperty("Content-Type", "application/json");

    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

    writer.write(doi);
    writer.flush();

    String line;
    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));



    while ((line = reader.readLine()) != null ) 
    {
        //Used to see of we get something from stream
        System.out.println(line);

        //Incoming is JSONArray, so create new array and parse fill it with incoming information
        JSONArray jsonArr = new JSONArray(line);
        JSONObject obj = jsonArr.getJSONObject(0);

        //Check if names from DBLP is the same as translators get
        //AuthorName, from field creators
        JSONArray authorNames = obj.getJSONArray("creators");
        ArrayList<Author> TranslationAuthors = new ArrayList<Author>();
然后被迫退出,因为它无法获得字段“创建者”,因为没有。
如何确保我的程序不会遇到此问题?我怎样才能轻松地将其放入我创建的错误日志文件中,而我无法收集任何信息。

我认为您正在使用一个
org.json.JSONObject
?如果是这样的话,有一个
has
方法,可以用来在密钥不存在的情况下避免
JSONException

 JSONArray authorNames = null;
 if (obj.has("creators")) {
   authorNames = obj.getJSONArray("creators");
 }

您可以尝试使用
obj.has(“创建者”)
,它返回一个布尔值,表示键是否在对象中。如果不是,则记录它。如果希望程序失败,请使用try-catch块尝试执行代码。如果失败,则记录它。
 JSONArray authorNames = null;
 if (obj.has("creators")) {
   authorNames = obj.getJSONArray("creators");
 }