package com.json.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class JsonUtils {
public static String getJsonContent(String urlStr)
{
try
{// 获取HttpURLConnection连接对象
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
// 设置连接属性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 获取相应码
int respCode = httpConn.getResponseCode();
if (respCode == 200)
{
return ConvertStream2Json(httpConn.getInputStream());
}
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
private static String ConvertStream2Json(InputStream inputStream)
{
String jsonStr = "";
// ByteArrayOutputStream相当于内存输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 将输入流转移到内存输出流中
try
{
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)
{
out.write(buffer, 0, len);
}
// 将内存流转换为字符串
jsonStr = new String(out.toByteArray());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
json.rar_gson
共45个文件
java:15个
class:15个
prefs:5个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 62 浏览量
2022-09-24
20:28:32
上传
评论
收藏 31KB RAR 举报
温馨提示
java有很多种处理json格式数据的框架,包括Gson,fastJson,Jackson,Json-lib,Json2014,都是源码,通过http请求获取对应的json,然后通过bean转化成对象。
资源推荐
资源详情
资源评论
收起资源包目录
json.rar (45个子文件)
json
TestGson
bin
com
json
test
TestGson.class 1KB
entity
Student.class 1KB
utils
JsonUtils.class 2KB
.settings
org.eclipse.jdt.core.prefs 629B
src
com
json
test
TestGson.java 468B
entity
Student.java 705B
utils
JsonUtils.java 2KB
.project 384B
.classpath 382B
TestFastJson
bin
com
json
test
TestFastJson.class 2KB
entity
Student.class 1KB
utils
JsonUtils.class 2KB
.settings
org.eclipse.jdt.core.prefs 629B
src
com
json
test
TestFastJson.java 2KB
entity
Student.java 700B
utils
JsonUtils.java 2KB
.project 388B
.classpath 479B
TestJsonLib
bin
com
json
test
TestJsonLib.class 1KB
entity
Student.class 1KB
utils
JsonUtils.class 2KB
.settings
org.eclipse.jdt.core.prefs 629B
src
com
json
test
TestJsonLib.java 521B
entity
Student.java 696B
utils
JsonUtils.java 2KB
.project 387B
.classpath 837B
TestJackson
bin
com
json
test
TestJackson.class 2KB
entity
Student.class 1KB
utils
JsonUtils.class 2KB
.settings
org.eclipse.jdt.core.prefs 629B
src
com
json
test
TestJackson.java 2KB
entity
Student.java 700B
utils
JsonUtils.java 2KB
.project 387B
.classpath 579B
TestJson2014
bin
com
json
test
TestJson2014.class 1KB
entity
Student.class 1KB
utils
JsonUtils.class 2KB
.settings
org.eclipse.jdt.core.prefs 629B
src
com
json
test
TestJson2014.java 628B
entity
Student.java 696B
utils
JsonUtils.java 2KB
.project 388B
.classpath 385B
共 45 条
- 1
资源评论
alvarocfc
- 粉丝: 105
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功