繁体   English   中英

漂亮的将字符串打印为JSON格式

[英]Pretty print String to JSON format

我执行一个AWS命令以检索现货价格历史记录。

DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest().withEndTime(start)
        .withInstanceTypes("m1.xlarge").withProductDescriptions("Linux/UNIX (Amazon VPC)").withStartTime(end);
        DescribeSpotPriceHistoryResult response = client.describeSpotPriceHistory(request);
        System.out.println(response.toString());

我获得了json格式的结果,但我收到了类似String的结果:

{SpotPriceHistory: [{AvailabilityZone: us-east-1d,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.035000,Timestamp: Wed Nov 07 00:18:50 CET 2018}, {AvailabilityZone: us-east-1c,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.035000,Timestamp: Wed Nov 07 00:18:50 CET 2018}, {AvailabilityZone: us-east-1b,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.035000,Timestamp: Wed Nov 07 00:18:50 CET 2018}, {AvailabilityZone: us-east-1a,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.035000,Timestamp: Wed Nov 07 00:18:50 CET 2018}, {AvailabilityZone: us-east-1e,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.350000,Timestamp: Thu Sep 20 01:08:39 CEST 2018}]}

我的问题是:如何改善结果的显示? 喜欢

        {
            "Timestamp": "2018-09-08T17:07:14.000Z", 
            "AvailabilityZone": "us-east-1d", 
            "InstanceType": "p2.16xlarge", 
            "ProductDescription": "Linux/UNIX", 
            "SpotPrice": "4.320000"
        }, 
        {
            "Timestamp": "2018-09-08T17:07:14.000Z", 
            "AvailabilityZone": "us-east-1c", 
            "InstanceType": "p2.16xlarge", 
            "ProductDescription": "Linux/UNIX", 
            "SpotPrice": "4.320000"
        }, 
        {
            "Timestamp": "2018-09-08T17:07:14.000Z", 
            "AvailabilityZone": "us-east-1b", 
            "InstanceType": "p2.16xlarge", 
            "ProductDescription": "Linux/UNIX", 
            "SpotPrice": "4.320000"
        }, 
        {
            "Timestamp": "2018-09-08T12:32:28.000Z", 
            "AvailabilityZone": "us-east-1e", 
            "InstanceType": "p2.16xlarge", 
            "ProductDescription": "Linux/UNIX", 
            "SpotPrice": "4.320000"
        }
    ]
}

您在响应对象上调用.toString() ,请谨慎操作,因为不能保证始终是json,正如您在上面看到的那样,它甚至不是有效的json,因为它缺少属性名称周围的引号和价值观。

获得所需内容的一种方法是调用response.getSpotPriceHistory()以获取现货价格数组,然后将其传递给ObjectMapper并将其编写为漂亮的字符串,如下所示:

public static void main(String[] args) throws IOException {

    AmazonEC2 client = AmazonEC2Client.builder().build();
    DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest()
        .withEndTime(new Date())
        .withInstanceTypes("m1.xlarge").withProductDescriptions("Linux/UNIX (Amazon VPC)")
        .withStartTime(new Date());
    DescribeSpotPriceHistoryResult response = client.describeSpotPriceHistory(request);

    ObjectMapper mapper = new ObjectMapper();
    String asPrettyJSon = mapper.writerWithDefaultPrettyPrinter()
        .writeValueAsString(response.getSpotPriceHistory());
    System.out.println(asPrettyJSon);
    }

两者都代表一个包含相同结构的jsonObjects的json数组。 显示结果将取决于您的前端实现,而不是jsonRespense的布局。

暂无
暂无

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

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