SpringMVC/SpringBoot JSON 包含整数和字符串的数组

SpringMVC/SpringBoot JSON Array with Integer and String

我正在尝试替换 returns JSON 的旧 ASP 页面,该页面填充了浮动图表。 (http://www.flotcharts.org/)

我的问题是,

旧 ASP 页 returns 这个 JSON:

{"xaxis":{"ticks":[[100,"Location1"],[200,"Location2"],[300,"Location3"]]}}

如您所见,"ticks" 包含一个包含 2 个成员的数组列表。每个数组的第一个成员是一个数字,第二个是一个字符串。

我能得到的最接近 SpringMVC/SpringBoot 的是:

{"xaxis":{"ticks":[["100","Location1"],["200","Location2"],["300","Location3"]]}}

您会注意到这些数字实际上是字符串。

这是我正在使用的控制器:

@Controller
public class GraphWebController {

    @RequestMapping(value = "/getXAxisData", method = RequestMethod.GET)
    public @ResponseBody Map<String, XAxisData> getXAxisData(Model model) {

        String[] location1 =  new String[]{"100", "Location1"};
        String[] location2 =  new String[]{"200", "Location2"};
        String[] location3 =  new String[]{"300", "Location3"};

        List<String[]> xAxisLocations = new ArrayList<String[]>();
        xAxisLocations.add(location1);
        xAxisLocations.add(location2);
        xAxisLocations.add(location3);

        XAxisData xaxis = new XAxisData(xAxisLocations);

        Map<String, XAxisData> xAxisData = new HashMap<String, XAxisData>();
        xAxisData.put("xaxis", xaxis);

        return xAxisData;

    }
}

public class XAxisData {

    private List<String[]> ticks;

    public XAxisData(List<String[]> ticks) {
        this.ticks = ticks;
    }

    public List<String[]> getTicks() {
        return ticks;
    }

    public void setTicks(List<String[]> ticks) {
        this.ticks = ticks;
    }
}

我尝试将字符串数组更改为对象数组,并将引用的数字更改为实际整数,但随后出现以下错误,即对象无法转换为字符串。

<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Tue May 26 14:28:55 EDT 2015</div><div>There was an unexpected error (type=Internal Server Error, status=500).</div><div>Could not write JSON: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; (through reference chain: java.util.HashMap[&quot;xaxis&quot;]-&gt;springmvc.graph.XAxisData[&quot;ticks&quot;]-&gt;java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; (through reference chain: java.util.HashMap[&quot;xaxis&quot;]-&gt;springmvc.graph.XAxisData[&quot;ticks&quot;]-&gt;java.util.ArrayList[0])</div></body></html>

如何获得与旧 ASP 页面完全相同的输出?

此时我对 hacky 解决方案相当开放。不过,越少 hacky 越好。谢谢!

假设 XAxisData class 如下所示

class XAxisData {
    private List<Object[]> ticks;

    public XAxisData(List<Object[]> xAxisLocations) {
        this.ticks = xAxisLocations;
    }

    public List<Object[]> getTicks() {
        return ticks;
    }

    public void setTicks(List<Object[]> ticks) {
        this.ticks = ticks;
    }
}

你可以使用

Object[] location1 = new Object[] { 100, "Location1" };
Object[] location2 = new Object[] { 200, "Location2" };
Object[] location3 = new Object[] { 300, "Location3" };

List<Object[]> xAxisLocations = new ArrayList<>();
xAxisLocations.add(location1);
xAxisLocations.add(location2);
xAxisLocations.add(location3);

XAxisData xaxis = new XAxisData(xAxisLocations);

Map<String, XAxisData> xAxisData = new HashMap<String, XAxisData>();
xAxisData.put("xaxis", xaxis);

这会产生

{"xaxis":{"ticks":[[100,"Location1"],[200,"Location2"],[300,"Location3"]]}}

如果您不打算拥有任何其他动态根 JSON 字段,我建议您将 Map 更改为 POJO。