1 將java對象類轉成json格式java
首先引入依賴jar文件json
注意依賴文件的版本號,高版本可能沒有對應的類服務器
2 個人實體類中包含內部類注意內部類要public才能被序列化成json格式app
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 導入包文件post
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;ui
4 序列化this
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 接口的調用,這個調用是posturl
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(); }