【问题标题】:How to set JSONArray in model attribute in spring modelandview controller?如何在 spring modelandview 控制器的模型属性中设置 JSONArray?
【发布时间】:2016-12-13 02:43:45
【问题描述】:

我想在 spring 的模型属性中添加 JSONArray 并从 ModelAndView 控制器返回。不能像下面那样做。请求帮助。谢谢!

@RequestMapping(value = "/sample.htm", method = RequestMethod.GET)
public ModelAndView seatRequest( ModelMap model,
        HttpServletRequest request,HttpServletResponse response, HttpSession session) throws JSONException, JsonProcessingException {

    JSONArray jsonArray = new JSONArray();
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("0", "val0");
    jsonObject.put("1", "val1");
    jsonObject.put("2", "val2");
    jsonArray.put(jsonObject);
    model.addAttribute("jsonData",jsonArray.toString());

    return new ModelAndView("sample");

【问题讨论】:

    标签: json spring spring-mvc model-view-controller


    【解决方案1】:

    你的意图是什么?目前 JsonArray 是以字符串形式添加的。您可以在视图中将此 jsonArray 作为字符串访问。但我认为你不想那样做。如果您想将其作为对象访问,只需添加为model.addAttribute("jsonData",jsonArray),然后您就可以在视图中将其作为对象访问。

    另外,在您的 json 对象中,键值为数字“0”、“1”等等。使其成为字母数字,即“key1”、“key2”等和值“val1”、“val2”等。因为标识符的名称不能以数字开头。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 2014-04-09
      • 2015-03-11
      • 1970-01-01
      相关资源
      最近更新 更多