有关将Python代码的JSON输出输出到单行的建议

时间:2018-10-29 18:46:15

标签: python json

您好,我正在进行自定义集成,以提取乘客数据并在多行中输出当前的JSON输出,我需要将其输出到单行中,但不确定我的代码在哪里出问题。

{“name”: “passenger-status”, “protocol_version”: “1”, “integration_version”: “1.0.0”, “metrics”: [{ “event_type”: “PassengerSample”, “Max pool size”: “4”,
“App groups”: “1”,
“Processes”: “2”,
“Requests in top-level queue”: “0”,
“Requests in queue”: “0”}]}

当我运行可执行文件时,我收到的输出是:

{“name”: “passenger-status”, “protocol_version”: “1”, “integration_version”: “1.0.0”, “metrics”: [{ “event_type”: “PassengerSample”, “Max pool size”: “4”, “App groups”: “1”, “Processes”: “2”, “Requests in top-level queue”: “0”, “Requests in queue”: “0”}]}

我需要的输出是:

@RequestMapping(value = "/form", method = RequestMethod.GET)
    public String form() {

        return "form";
    }


    @RequestMapping(value = "/doUp", method = RequestMethod.GET)
    public String subirFoto(@RequestParam("file") File file) {

                byte[] imageData = new byte[(int) file.length()];

                try {
                    FileInputStream fileInputStream = new FileInputStream(file);
                    fileInputStream.read(imageData);
                    fileInputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Photo image = new Photo();
                image.setNombre("test.jpg");
                image.setFoto(imageData);

                try {
                    photoService.insertPhoto(image);
                } catch (InstantiationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                return "success";
    }

1 个答案:

答案 0 :(得分:1)

是多行打印还是单行打印属性都无关紧要,元素的顺序也无关紧要。话虽如此,

json.dumps(json.loads(stats)) 

无需编写自定义解析器就可以解决问题。