java json 解析null,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 array = new ArrayList();

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值