简体   繁体   English

如何在Spring MVC Controller中循环遍历JSON响应

[英]How to loop through JSON response in Spring MVC Controller

I will like to use the JSON response inside a controller. 我想在控制器内使用JSON响应。 I am calling a method that returns the JSON. 我正在调用返回JSON的方法。 See my code below. 请参阅下面的代码。 Please how do I loop through the Json object returned inside my controller. 请如何循环遍历控制器内部返回的Json对象。 I need to use the properties like sending mail to the email addresses from another method inside my controller . 我需要使用属性,例如从控制器内部的另一种方法向电子邮件地址发送邮件。

My method that does that returns the JSON : 我的方法会返回JSON:

@ResponseBody
private ResponseEntity<?> queryIsw(String ref, String amt) throws Exception{
    String pdtid = "62";
    String salt = "D3D1D05AFE42AD50818167EAC73C109168A0F108";
    RestTemplate restt = new RestTemplate();
    String uri = "https://bestng.com/gettransaction.json";
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    params.add("productid", pdtid);
    params.add("transactionreference", ref);
    params.add("amount", amt);
    UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(uri).queryParams(params).build();
    URI oro = uriComponents.toUri();
    HttpHeaders hea = new HttpHeaders();
    String hs = pasher(pdtid, ref, salt);
    hea.add("hash", hs);
    HttpEntity<String> hent = new HttpEntity<String>(hea);

    ResponseEntity<Object> resp =  restt.exchange(oro, HttpMethod.GET, hent, Object.class);     

    return resp;

}

Below is my call to this method above from another method : 下面是我从另一个方法对上述方法的调用:

ResponseEntity<?> dres = queryIsw(dref,ama);

Kindly explain how I can use properties of 'dres' returned in my controller . 请解释一下如何使用控制器中返回的'dres'属性。

Thanks 谢谢

Try taking a look at the Jackson JSON to Java mapping tools and specifically the ObjectMapper. 尝试看一下Jackson到Java的JSON映射工具,尤其是ObjectMapper。 It can convert a properly formatted JSON string into an object hierarchy from which you can pull out the data that you need. 它可以将格式正确的JSON字符串转换为对象层次结构,您可以从该对象层次结构中提取所需的数据。 Jackson is a frequently used tool for this activity. 杰克逊是此活动的常用工具。 Take a look at the tutorial for more details: 查看该教程以获取更多详细信息:

http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/ http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

If you need more help, do ask. 如果您需要更多帮助,请询问。

I am assuming that it will return within the body of the ResponseEntity. 我假设它将在ResponseEntity的主体内返回。

Try: 尝试:

String body = dres.getBody();

You can use something that can parse that string to a json object. 您可以使用将字符串解析为json对象的方法。 Something like: 就像是:

JSONObject jObject  = new JSONObject(body);

See: 看到:

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

Java String to JSON conversion Java String到JSON的转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM