简体   繁体   中英

How to return string as json from Spring MVC 3 Controller?

I'm using Spring mvc 3.0.1. I want to return json type but although Why I use @Responsebody return text/plain;charset=ISO-8859-1. How can I return 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.

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;

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();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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