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(" "); } } 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(); }