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

时间:2018-11-07 21:02:29

标签: java json amazon-web-services amazon-ec2

我执行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"
        }
    ]
}

2 个答案:

答案 0 :(得分:3)

您在响应对象上调用.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);
    }

答案 1 :(得分:0)

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