如何从 Spring MVC 3 控制器 return 字符串为 json?
How to return string as json from Spring MVC 3 Controller?
我正在使用 Spring mvc 3.0.1。我想 return json 类型,但是为什么我使用 @Responsebody return text/plain;charset=ISO-8859-1。我怎样才能returnjson?我看了其他所有答案,但找不到正确答案。
请注意:在一些答案中我看到使用了 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>
添加如下代码后,returnsjson正常;
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();
}
我正在使用 Spring mvc 3.0.1。我想 return json 类型,但是为什么我使用 @Responsebody return text/plain;charset=ISO-8859-1。我怎样才能returnjson?我看了其他所有答案,但找不到正确答案。
请注意:在一些答案中我看到使用了 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>
添加如下代码后,returnsjson正常;
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();
}