![](/img/trans.png)
[英]Spring MVC - How to return simple String as JSON in Rest Controller
[英]How to return string as json from Spring MVC 3 Controller?
我正在使用 Spring mvc 3.0.1。 我想返回 json 類型,但盡管為什么我使用 @Responsebody 返回 text/plain;charset=ISO-8859-1。 如何退回 json? 我查看了其他所有答案,但找不到正確答案。
請注意:在我看到的一些答案中,使用了produces = MediaType.APPLICATION_JSON_VALUE,但是在這個spring 版本中我不能使用它。
謝謝你的幫助
代碼:
@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>
<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>
添加以下代碼后,正確返回 json;
JSONObject jsonResponse = new JSONObject();
JSONArray jsonArrayResponse= new JSONArray();
jsonResponse.put("CURRENCY", jsonArray);
jsonArrayResponse.put(jsonResponse);
jsonArrayResponse.toString();
完整代碼如下;
@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.