简体   繁体   English

在Spring Boot中将JSON文件作为JSONArray返回

[英]returning JSON file as JSONArray in Spring Boot

I am developing a Spring Boot application in which I have a JSON properties file: 我正在开发一个Spring Boot应用程序,其中有一个JSON属性文件:

{
    data: [
        {"firstWebServiceUrl":"http://localhost:8080/firstUrl/"},
        {"secondWebServiceUrl":"http://localhost:8080/secondUrl/"},
    ]
}

I have created a controller to read and return this file as org.json.JSONArray object: 我创建了一个控制器来读取此文件并将其作为org.json.JSONArray对象返回:

@RestController
@RequestMapping("/Test")
public class MainController {

    @RequestMapping(value = "/getJsonProperties", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSONArray getJsonProperties() {
        Resource resource = new ClassPathResource("/static/configs/properties.json");
        InputStream resourceAsStream;
        JSONObject jsonObject = null;
        JSONArray jsonArray = null;
        String resourceAsString = null;
        try {
            resourceAsStream = resource.getInputStream();
            byte[] resourceInBytes = IOUtils.toByteArray(resourceAsStream);
            resourceAsString = new String(resourceInBytes);
            jsonObject = new JSONObject(resourceAsString);          
            jsonArray = jsonObject.getJSONArray("data");
            System.out.println("json array object is " + jsonArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

}

When i call this method I see the output in the console (thanks to sysout): 当我调用此方法时,我会在控制台中看到输出(感谢sysout):

json array object is [{"firstWebServiceUrl":"http://localhost:8080/firstUrl/"},{"secondWebServiceUrl":"http://localhost:8080/secondUrl/"}]

as expected. 如预期的那样。 However when I call this method from the browser by entering the url: http://localhost:8090/Test/getJsonProperties I get the output: 但是,当我通过输入URL从浏览器调用此方法时: http://localhost:8090/Test/getJsonProperties我得到了输出:

{"empty":false}

instead of the contents. 而不是内容。 How can I return the contents? 如何退货?

Return a String instead of JSONArray because there is no default converter that could serialize a JSONArray into a JSON string: 返回一个String而不是JSONArray,因为没有默认转换器可以将JSONArray序列化为JSON字符串:

@RestController
@RequestMapping("/Test")
public class MainController {

    @RequestMapping(value = "/getJsonProperties", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String getJsonProperties() {
        Resource resource = new ClassPathResource("/static/configs/properties.json");
        InputStream resourceAsStream;
        JSONObject jsonObject = null;
        JSONArray jsonArray = null;
        String resourceAsString = null;
        try {
            resourceAsStream = resource.getInputStream();
            byte[] resourceInBytes = IOUtils.toByteArray(resourceAsStream);
            resourceAsString = new String(resourceInBytes);
            jsonObject = new JSONObject(resourceAsString);          
            jsonArray = jsonObject.getJSONArray("data");
            System.out.println("json array object is " + jsonArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray != null ? jsonArray.toString() : "[]";
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM