JAVA抓取网页json数据时打印结果乱码求帮助

WionWan WionWan 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近做个小应用,需要抓取一个网站的数据没错就是B站。。。
然后我得到了b站的一段json数据网址,准备抓取下来做解析,但是呢,问题来了,我用以下方法抓取的时候,打印内容是乱码的,B站原来的数据是Unicode编码,我用了很多方法转换字符集都不能成功,各位大神支支招吧!写了
package com.dmj.test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class Testbilibili {
public static void main(String[] args) {
InputStream iis=null;
try {
URL url=new URL("http://www.bilibili.com/index/rank/all-7-33.json");
url.openConnection();
iis=url.openStream();

byte[] bs=new byte[1024];
int length=0;
StringBuffer sb=new StringBuffer();
while((length=iis.read(bs,0,bs.length))!=-1){
sb.append(new String(bs,0,length));
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
iis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

【以上方法乱码】
看了网上的教程,通过
String value=new String(sb.toString().getBytes("Unicode"),"utf-8");
System.out.println(value);

【以上方法乱码】依然不行。我换成 HttpURLConnection 抓取网页 设置
httpurlconnection.setRequestProperty("Content-type", "text/html");
httpurlconnection.setRequestProperty("Accept-Charset", "utf-8");
httpurlconnection.setRequestProperty("contentType", "utf-8");

【以上方法乱码】读取的依然乱码。
那么小弟在此请教各位,我想正确的抓取 http://www.bilibili.com/index/rank/all-7-33.json 这个网页的json数据打印下来不乱码或者显示正确的Unicode的也行只要不乱吗就好。。求支招。。。。如果可以您先试试方法成功了之后再告诉我一下,,。。小弟拜托各位啦。
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
WionWan
Java 51,407 成员 86,130 内容