[英]How to return string as json from Spring MVC 3 Controller?
I'm using Spring mvc 3.0.1.我正在使用 Spring mvc 3.0.1。 I want to return json type but although Why I use @Responsebody return text/plain;charset=ISO-8859-1.
我想返回 json 类型,但尽管为什么我使用 @Responsebody 返回 text/plain;charset=ISO-8859-1。 How can I return json?
如何退回 json? I looked other all answers but I could not find right answer.
我查看了其他所有答案,但找不到正确答案。
Note that: In some answers I've seen that produces = MediaType.APPLICATION_JSON_VALUE is used, but in this spring version I couldnt use that.请注意:在我看到的一些答案中,使用了produces = MediaType.APPLICATION_JSON_VALUE,但是在这个spring 版本中我不能使用它。
Thanks for your help谢谢你的帮助
Code:代码:
@RequestMapping(value = "/XXX")
@ResponseBody
public String getResponse(String object) {
JSONObject jsonCurrencyObject = new JSONObject(object);
JSONObject jsonObject;
@SuppressWarnings("rawtypes") Iterator iterator = jsonCurrencyObject.keys();
JSONArray jsonArray = new JSONArray();
while (iterator.hasNext()) {
String key = (String) iterator.next();
jsonArray.put(jsonCurrencyObject.get(key));
}
String last;
String change;
String fxCode;
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
last = BigDecimal.valueOf((Double) jsonObject.get("last"))
.setScale(4, BigDecimal.ROUND_HALF_UP).toPlainString();
jsonObject.put(MarketsCurrencyConstants.LAST, last);
change = new BigDecimal(jsonObject.getString("prnc")).toPlainString();
jsonObject.put(MarketsCurrencyConstants.CHANGE, change.replace(".", ","));
if (!jsonObject.isNull("fxCode")) {
fxCode = jsonObject.getString("fxCode");
jsonObject.put(MarketsCurrencyConstants.NEW_FX_CODE, manipulateFxCode(fxCode));
}
}
logger.info("jsonArray string: " + jsonArray.toString());
return jsonArray.toString();
}
Dependency;依赖;
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.1.RELEASE-A</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
After added following code, it returns json properly;添加以下代码后,正确返回 json;
JSONObject jsonResponse = new JSONObject();
JSONArray jsonArrayResponse= new JSONArray();
jsonResponse.put("CURRENCY", jsonArray);
jsonArrayResponse.put(jsonResponse);
jsonArrayResponse.toString();
Full code is as following;完整代码如下;
@RequestMapping(value = "/XXX")
@ResponseBody
public String getResponse(String object) {
JSONObject jsonCurrencyObject = new JSONObject(object);
JSONObject jsonObject;
@SuppressWarnings("rawtypes") Iterator iterator = jsonCurrencyObject.keys();
JSONArray jsonArray = new JSONArray();
while (iterator.hasNext()) {
String key = (String) iterator.next();
jsonArray.put(jsonCurrencyObject.get(key));
}
String last;
String change;
String fxCode;
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
last = BigDecimal.valueOf((Double) jsonObject.get("last"))
.setScale(4, BigDecimal.ROUND_HALF_UP).toPlainString();
jsonObject.put(MarketsCurrencyConstants.LAST, last);
change = new BigDecimal(jsonObject.getString("prnc")).toPlainString();
jsonObject.put(MarketsCurrencyConstants.CHANGE, change.replace(".", ","));
if (!jsonObject.isNull("fxCode")) {
fxCode = jsonObject.getString("fxCode");
jsonObject.put(MarketsCurrencyConstants.NEW_FX_CODE, manipulateFxCode(fxCode));
}
}
logger.info("jsonArray string: " + jsonArray.toString());
JSONObject jsonResponse = new JSONObject();
JSONArray jsonArrayResponse= new JSONArray();
jsonResponse.put("CURRENCY", jsonArray);
jsonArrayResponse.put(jsonResponse);
return jsonArrayResponse.toString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.