在Spring中显示JSON多维数组

时间:2023-01-21 21:28:09

Struggling to display JSON array in below format using Spring Controller.

正在努力使用Spring Controller以下面的格式显示JSON数组。

[
    ["Apple","Red",[100,10]],
    ["Orange","Orange",[150,8]]
]

I am using Jackson library.

我正在使用杰克逊图书馆。

My controller is:

我的控制器是:

@Controller
@RequestMapping("/fruits")
public class FruitController {

    @RequestMapping(value="{id}", method = RequestMethod.GET)
    public @ResponseBody Fruit getLocationInJSON(@PathVariable String id) {

        Fruit fruit = new Fruit();

        fruit.setName("Apple");
        fruit.setColor("Huningue");
        fruit.setNumbers(new String[]{"100", "10"});

        return fruit;
    }
}

Thank you in advance,

先谢谢你,

Selvaraj

Selvaraj

1 个解决方案

#1


0  

The format you are trying to achieve is not applicable in java, see java does not support array of several types, you can have either array of strings or array of arrays but you cannot have array of both strings and arrays

您尝试实现的格式不适用于java,请参阅java不支持几种类型的数组,您可以使用字符串数组或数组数组,但不能包含字符串数组和数组

#1


0  

The format you are trying to achieve is not applicable in java, see java does not support array of several types, you can have either array of strings or array of arrays but you cannot have array of both strings and arrays

您尝试实现的格式不适用于java,请参阅java不支持几种类型的数组,您可以使用字符串数组或数组数组,但不能包含字符串数组和数组