我有个问题,我想做一个网络服务来获取Json。
我的json是这样的:
[{"id":"test","prix":"0,76"},{"id":"test2","prix":"0,76"},{"id":"BODOUR","prix":"0,76"}]
我的代码:
@GetMapping(value="/setup/tarif/getTarifElectricity")
public JSONArray getTarifGrid() throws IOException, SAXException {
String filePath = Common.filePathData("setup.tarif", "tarif.txt");
byte[] bytes = Files.readAllBytes(Paths.get(filePath));
String tarifGrid = new String(bytes,"UTF-8");
JSONArray jsonArr = new JSONArray(tarifGrid);
System.out.println(jsonArr.toString());
return jsonArr;
}
但当我想测试我的网络服务时,我的结果是:
{"empty": false}
localhost:8080/setup/tarif/getTarifElectricity在chrome显示器上{"empty":false}而不是我的JsonArray
我的SYSOUT返回我:
[{"prix":"0,76","id":"test"},{"prix":"0,76","id":"test2"},{"prix":"0,76","id":"BODOUR"}]
感谢
您是否已经尝试从该方法返回String?我的意思是:
return jsonArr.toString();
在我看来,原因是JSONArray对象只包含信息,无论它是否为空。所以Jackson只使用这些信息映射这个对象的JSON表示。但如果你想获得所有数据,你需要使用jsonArr.toString();