java爬虫解析json出现net.sf.json.JSONException: null object

新手上路,请多包涵

package com.lxd.text;

import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.ParseException;

import com.lxd.util.Content;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class CommentText {

public static void main(String[] args) throws ParseException, IOException {
    Content httpClientUtil = new Content();
    String url = "http://comment.163.com/api/v1/products/a2869674571f77b5a0867c3d71db5856/threads/DB6T5JK500058784/comments/newList?offset=0&limit=30&showLevelThreshold=72&headLimit=1&tailLimit=2&ibc=newspc&_=1518169262707:formatted"; // 百度首页

    String content = httpClientUtil.getUrlSourse(url); // 得到百度网页源码
    System.out.println(content);
    //String content=contents.replaceAll(":null,", ":\"null\","); 
    System.out.println("------------------------------------------------------------------------");

    //JSONObject jsonObject = JSONObject.fromObject(content);
    JSONObject  jsonObject =JSONObject.fromObject(content);

    JSONArray j = (JSONArray) jsonObject.get("commentIds");
    ArrayList<String> array = new ArrayList<String>();
    for (int i = 0; i < j.size(); i++) {
        array.add(j.getString(i));
        System.out.println(j.getString(i));
    }
    // System.out.println(array);
    Object objComments = jsonObject.get("comments");
    for (int i = 0; i < array.size(); i++) {

        jsonObject = JSONObject.fromObject(objComments);

        Object objCommentIds = jsonObject.get(array.get(i));
        jsonObject = JSONObject.fromObject(objCommentIds);

        Object objContent = jsonObject.get("content");
        Object objCreatTime = jsonObject.get("createTime");
        Object objUser = jsonObject.get("user");
        jsonObject = JSONObject.fromObject(objUser);
        Object objNickname = jsonObject.get("nickname");
        System.out.println("网友名:" + objNickname + " 评论内容:" + objContent + "评论时间:" + objCreatTime);
    }
}

}

以下事运行结果,和错误:

网友名:null 评论内容:抵制、退出、死不参赛。评论时间:2018-02-22 15:55:03
网友名:有态度网友071f_I 评论内容:怨妇无休无止,有理也成无赖!评论时间:2018-02-22 15:51:35
网友名:狄安娜的绝望 评论内容:这冬奥会说白了 真正愿意投入举办的国家不多,能有举办的就不错了,肯定的照顾人家棒子了评论时间:2018-02-22 15:50:50
Exception in thread "main" net.sf.json.JSONException: null object

at net.sf.json.JSONObject.verifyIsNull(JSONObject.java:2744)
at net.sf.json.JSONObject.get(JSONObject.java:1957)
at com.lxd.text.CommentText.main(CommentText.java:41)

阅读 4.2k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题