具有整数和字符串的SpringMVC / SpringBoot JSON数组

时间:2015-05-26 18:31:35

标签: java json spring-mvc spring-boot flot

我正在尝试替换一个返回填充了flot图表的JSON的旧ASP页面。 (http://www.flotcharts.org/

我的问题是这个,

旧的ASP页面返回此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;
    }
}

我尝试将String数组更改为Object数组,并将引用的数字更改为实际整数,但后来我得到以下错误:Object无法转换为String。

<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解决方案持开放态度。虽然越少越好。谢谢!

1 个答案:

答案 0 :(得分:2)

假设XAxisData类如下

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。