1 will turn into a java object class format json
First introduced dependent jar files
Note dependency file version numbers may not correspond to the high-class version
2 of my entity class contains inner classes pay attention to public inner class can be serialized to json format
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 Package File
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
4 sequence of
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 results:
{ "args": { "nameList": [ "AIR PRODUCTS & CHEMICALS INC", "AMAZON.COM INC", "ALTRIA GROUP INC", "APPLE INC", "ALPHABET INC" ] }, "filter": { "key": "EquityNameMatching" } }
6 interface call, the call is 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) { // off before connecting to the server automatically redirect connect.setFollowRedirects ( to false ); } // set request output stream connect.setDoOutput ( to true ); // build request to write data in the stream write request body the OutputStream = OUT connect.getOutputStream (); the try (= Write the PrintWriter new new the PrintWriter (OUT)) { Boolean First = to true ; for (the 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" ; } // If the redirect is greater than 0 indicates a redirection IF (redirescts> 0 ) { // response code acquisition request int responseCode = connect.getResponseCode (); IF (== responseCode the HttpURLConnection == || responseCode .HTTP_MOVED_PERM HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_SEE_OTHER) { // Get the redirected location String connect.getHeaderField = lOCATION ( "the location" ); IF ! (lOCATION = null ) { Base the java.net.URL = connect.getURL (); // disconnect this connection connect.disconnect (); return the doPost ( new new the URL (Base, LOCATION), namevaluepairs, the userAgent, redirescts -. 1 ); } } } the else IF (redirescts == 0 ) { the throw new new IOException ( "redirection can not handle too much" ); } // Get the response stream the InputStream responseStream = connect.getInputStream (); the try (Scanner in = new 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(); }