简体   繁体   English

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

[英]Pretty print String to JSON format

I execute an AWS command to retrieve the spot price history. 我执行一个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());

I obtained the result in json format but I receive it in String like: 我获得了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}]}

my question is: How can I improve the displaying of the results ? 我的问题是:如何改善结果的显示? like 喜欢

        {
            "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"
        }
    ]
}

You're calling the .toString() on the response object, just be careful with this as there is no guarantee that will always be json, as you're seeing above, it's not even valid json as it's missing quotes around the attribute names and values. 您在响应对象上调用.toString() ,请谨慎操作,因为不能保证始终是json,正如您在上面看到的那样,它甚至不是有效的json,因为它缺少属性名称周围的引号和价值观。

One option to get what you want is to call response.getSpotPriceHistory() to get you the array of spot prices, then pass that thru ObjectMapper and write it as a pretty string, like so: 获得所需内容的一种方法是调用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);
    }

Both represent a json array containing jsonObjects of same structure. 两者都代表一个包含相同结构的jsonObjects的json数组。 Displaying result will depend on your front implementation not the layaout of your jsonRespense. 显示结果将取决于您的前端实现,而不是jsonRespense的布局。

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

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