1 将java对象类转成json格式

首先引入依赖jar文件

注意依赖文件的版本号,高版本可能没有对应的类

2 我的实体类中包含内部类注意内部类要public才能被序列化成json格式

import java.util.List;

public class EuityParam {

    public EuityParam(String filter,List<String> args)
{
super();
this.filter=new Param0(filter);
this.args=new Param1(args);
} private Param1 args; public Param1 getArgs()
{
return args;
}
public void setArgs(Param1 _Param1)
{
this.args=_Param1;
} private Param0 filter; public Param0 getFilter()
{
return filter;
}
public void setFilter(Param0 _param0)
{
this.filter=_param0;
}
public class Param1
{
public List<String> getNameList() {
return nameList;
}
public void setNameList(List<String> nameList) {
this.nameList = nameList;
}
public Param1(List<String> nameList)
{
this.nameList=nameList;
}
private List<String> nameList; }
public class Param0
{
public Param0(String key)
{
this.key=key;
}
private String key; public String getKey()
{
return key;
}
public void setKey(String key)
{
this.key=key;
}
} }

3 导入包文件

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

4 序列化

        List<String> nameLst=new LinkedList<String>();
nameLst.add("AIR PRODUCTS & CHEMICALS INC");
nameLst.add("AMAZON.COM INC");
nameLst.add("ALTRIA GROUP INC");
nameLst.add("APPLE INC");
nameLst.add("ALPHABET INC");
EuityParam euityParam=new EuityParam("EquityNameMatching", nameLst); JSONObject jsonData= JSONObject.fromObject(euityParam);
System.out.println(jsonData);

5 结果:

{
"args": {
"nameList": [
"AIR PRODUCTS & CHEMICALS INC",
"AMAZON.COM INC",
"ALTRIA GROUP INC",
"APPLE INC",
"ALPHABET INC"
]
},
"filter": {
"key": "EquityNameMatching"
}
}

6 接口的调用,这个调用是post

private static String doPost(URL url, Map<Object, Object> nameValuePairs, String userAgent, int redirescts)
throws IOException {
// TODO Auto-generated method stub
StringBuilder response = new StringBuilder();
// HttpURLConnection可以处理重定向
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
if (userAgent != null) {
connect.setRequestProperty("Authorization", userAgent);//设置http请求头
}
if (redirescts >= 0) {
// 连接到服务器之前关闭自动重定向
connect.setFollowRedirects(false);
}
// 设置请求的输出流
connect.setDoOutput(true);
// 构建往请求body内写入请求数据的写入流
OutputStream out = connect.getOutputStream();
try (PrintWriter write = new PrintWriter(out)) {
boolean first = true;
for (Entry<Object, Object> pair : nameValuePairs.entrySet()) {
if (first) {
first = false;
} else {
write.print('&');
}
String name = pair.getKey().toString();
String value = pair.getValue().toString();
write.print(name);
write.print('=');
write.print(URLEncoder.encode(value, "UTF-8"));
}
} // 获取请求的编码类型
String encoding = connect.getContentEncoding();
if (encoding == null) {
encoding = "UTF-8";
} // 如果redirect大于0表明有重定向
if (redirescts > 0) {
// 获取请求的响应码
int responseCode = connect.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP
|| responseCode == HttpURLConnection.HTTP_SEE_OTHER) { // 获取重定向的位置
String location = connect.getHeaderField("Location");
if (location != null) {
java.net.URL base = connect.getURL();
// 断开本次连接
connect.disconnect();
return doPost(new URL(base, location), nameValuePairs, userAgent, redirescts - 1);
}
}
} else if (redirescts == 0) {
throw new IOException("重定向太多无法处理");
} // 获取响应流
InputStream responseStream = connect.getInputStream();
try (Scanner in = new Scanner(responseStream, encoding)) {
while (in.hasNextLine()) {
response.append(in.nextLine());
response.append("\n");
}
}
catch (Exception e)
{
// TODO: handle exception
InputStream err = connect.getErrorStream();
if (err == null) {
throw e;
}
try (Scanner in = new Scanner(err)) {
response.append(in.hasNextLine()); }
}
return response.toString();
}

最新文章

  1. Jplayer歌词同步显示插件
  2. 什么时候用position
  3. backBarButtonItem 颜色/文字修改
  4. AdventureWorksDW2008R2 attach: Unable to open the physical file. Operating system error 5: &quot;5(Access is denied.)
  5. 【HTML5 1】39个让你受益的HTML5教程
  6. Nginx 301重定向域名
  7. easy ui 表单元素input控件后面加说明(红色)
  8. JavaScript 对象属性的遍历
  9. OSG+VS2010+win7环境搭建
  10. EasyUI基础searchbox&amp;amp;progressbar(搜索框,进度条)
  11. elastic search 学习 一
  12. error and solve
  13. Django学习笔记之视图高级-类视图
  14. TP微信扫码支付
  15. HDU3046_Pleasant sheep and big big wolf
  16. nagios监控mysql
  17. 网站缓存技术(Redis、Memcached、Ehcache)
  18. 关于Manjaro与Ubuntu双系统并存引发的一个boot问题
  19. 推荐几个MySQL大牛的博客
  20. Java JVM:内存溢出(栈溢出,堆溢出,持久代溢出以及 nable to create native thread)

热门文章

  1. 基于Source Insight_Scan的C/C++静态代码检查工具安装说明
  2. [译]使用Pandas读取大型Excel文件
  3. php 3.2 生成压缩文件,并下载
  4. MySQL插入大批量数据时报错“The total number of locks exceeds the lock table size”的解决办法
  5. SQL Delta实用案例介绍,很好的东西,帮了我不少忙
  6. JavaScript获取手机屏幕翻转方法
  7. DevOps - DevOps精要 - 溯源
  8. etcd学习之安装与命令
  9. IntelliJ IDEA 下搭建vue项目工程
  10. Linux-通过源码安装某个版本的内核