关于第三方接口调用返回值问题?

发布于 2021-12-02 04:50:36 字数 3047 浏览 824 评论 7

这是第三方接口返回值类型

{

"data": {

"standard": "RO-185I智能-新款",

"filter_period": "",

"channel": "电商",

"type": "1",

"u9_code": "104011-0003",

"pro_list": "",

"pro_install_type": "",

"model": "",

"info": "",

"pro_filter9": "",

"pro_filter8": "",

"pro_filter7": "",

"pro_filter6": "",

"pro_filter5": "190005-0048",

"pro_filter4": "190007-0013",

"pro_filter3": "190005-0002",

"pro_filter2": "190004-0004",

"pro_filter1": "190002-0003",

"water_effect_level": "",

"weight": "",

"market_date": "2017.11",

"suit_filter_count": "",

"hygiene_cert": "",

"size": "197*386*464",

"name": "RO-185I智能-新款",

"pro_filter10": "",

"pro_level": "",

"status": "1",

"suit_filter_list": "",

"electric_cert": "",

"pic1": "",

"is_iot": "",

"pro_org": "",

"pic2": "",

"suning_code": "",

"pic3": "",

"amount": "",

"pro_water": "",

"pro_sublist": "",

"pro_flux": "",

"need_install": "1",

"pro_power": "",

"jingdong_code": "",

"u8_code": "703100"

},

"status": {

"code": "E0",

"message": "成功"

}

}

我请求后的数据为:String类型的字符串。

 

如何返回解析成指定的格式?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

彩扇题诗 2021-12-08 13:09:04

看看 OpenFeign 的例子:

interface GitHub {
  @RequestLine("GET /repos/{owner}/{repo}/contributors")
  List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}

public static class Contributor {
  String login;
  int contributions;
}

public class MyApp {
  public static void main(String... args) {
    GitHub github = Feign.builder()
                         .decoder(new GsonDecoder())
                         .target(GitHub.class, "https://api.github.com");
  
    // Fetch and print a list of the contributors to this library.
    List<Contributor> contributors = github.contributors("OpenFeign", "feign");
    for (Contributor contributor : contributors) {
      System.out.println(contributor.login + " (" + contributor.contributions + ")");
    }
  }
}

是不是很酷?

悲喜皆因你 2021-12-08 12:51:00

写一个实体类,对应第三方接口的返回的JSON数据格式。

用JSON解析数据,得到实体类,返回实体类给业务调用着。

如果很多这样的接口,建议你试试#OpenFeign# https://github.com/OpenFeign/feign

这货可以帮你省很多事。

等风来 2021-12-08 12:47:56

JsonObject

居里长安 2021-12-08 10:18:46

CloseableHttpClient 可以用单例,重复创建浪费资源。

倚栏听风 2021-12-06 14:46:24

HTTP请求代码:

public class HttpClientUtil {
    /**
     * 带参数的get请求
     * @param url
     * @param param
     * @return String
     */
    public static String doGet(String url, Map<String, String> param) {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        String resultString = "";
        CloseableHttpResponse response = null;
        try {
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
                for (String key : param.keySet()) {
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.build();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(uri);
            // 执行请求
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }
彩扇题诗 2021-12-04 16:15:11
@Override
public String findProductsAndFilters(String code) {

    Map<String,String> params = new LinkedHashMap<>();
   //数据查询账户
    String aid=CemUtils.OrderInter.AID_;
    //数据提交时间戳
    long t = System.currentTimeMillis()/1000;
    String ts =String.valueOf(t);
    StringBuilder builder = new StringBuilder("");
    builder.append("aid").append(aid).append("procode").append(code).append("ts").append(ts).append("63f737a925728cfe");
    String sign = MD5Util.MD5Encode(builder.toString(),"utf-8");
    String url = "XXX";
    params.put("aid", CemUtils.OrderInter.AID_);
    params.put("procode",code);
    params.put("ts",ts);
    params.put("sign",sign);


    String result = HttpClientUtil.doGet(url, params);
    return result;
}

 

等风来 2021-12-02 14:38:09

接口代码:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文