简体   繁体   中英

Pretty print String to JSON format

I execute an AWS command to retrieve the spot price history.

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:

{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.

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:

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. Displaying result will depend on your front implementation not the layaout of your jsonRespense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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