实现json格式的字符串排序
String str = "{\"receiver\":\"alert-center-receiver\",\"status\":\"firing\",\"alerts\":[{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8103\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8103 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8103 上的微服务已停止服务\"},\"startsAt\":\"2020-05-13T15:26:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"0bde86236a601261\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8104\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8104 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8104 上的微服务已停止服务\"},\"startsAt\":\"2020-05-14T13:06:56.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"387a2996a9cbaf0e\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8105\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8105 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8105 上的微服务已停止服务\"},\"startsAt\":\"2020-04-29T16:15:56.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"2a39af223b9721cb\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8106\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8106 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8106 上的微服务已停止服务\"},\"startsAt\":\"2020-04-29T16:16:26.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"86ff7d3260acb1d8\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8107\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8107 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8107 上的微服务已停止服务\"},\"startsAt\":\"2020-05-13T11:43:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"95ad35c0e89f1005\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8108\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8108 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8108 上的微服务已停止服务\"},\"startsAt\":\"2020-05-13T15:26:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"17fcb91c34384a22\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8109\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8109 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8109 上的微服务已停止服务\"},\"startsAt\":\"2020-05-14T14:00:41.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"1cb9f5722e9b371f\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8110\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8110 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8110 上的微服务已停止服务\"},\"startsAt\":\"2020-05-13T15:26:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"5778c60cb8e13c51\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8111\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8111 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8111 上的微服务已停止服务\"},\"startsAt\":\"2020-04-29T16:17:26.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"40b36794a98a7414\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8113\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8113 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8113 上的微服务已停止服务\"},\"startsAt\":\"2020-05-13T15:26:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"22641d60acd7333a\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8131\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8131 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8131 上的微服务已停止服务\"},\"startsAt\":\"2020-05-13T16:00:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"114a5c5c51b0f446\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8202\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8202 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8202 上的微服务已停止服务\"},\"startsAt\":\"2020-05-14T12:41:41.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"f72e3d41496bbf8f\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8314\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8314 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8314 上的微服务已停止服务\"},\"startsAt\":\"2020-05-14T11:41:41.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"e30609afa94735c7\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.12:8870\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.12:8870 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.12:8870 上的微服务已停止服务\"},\"startsAt\":\"2020-05-07T16:25:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"5002a1b12882f3a2\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.41:10020\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.41:10020 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.41:10020 上的微服务已停止服务\"},\"startsAt\":\"2020-05-12T15:15:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"0e247dcc322ba410\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.41:8089\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.41:8089 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.41:8089 上的微服务已停止服务\"},\"startsAt\":\"2020-05-13T15:26:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"c89b1a9d2457a452\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.41:8090\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.41:8090 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.41:8090 上的微服务已停止服务\"},\"startsAt\":\"2020-04-27T10:53:41.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"04184d6d3c3d9480\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.41:8098\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.41:8098 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.41:8098 上的微服务已停止服务\"},\"startsAt\":\"2020-05-14T09:43:56.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"0a4c499b2d94a688\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.41:9093\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.41:9093 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.41:9093 上的微服务已停止服务\"},\"startsAt\":\"2020-05-14T09:44:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"97fd5841dd15b51c\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.41:9094\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.41:9094 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.41:9094 上的微服务已停止服务\"},\"startsAt\":\"2020-05-14T09:44:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"c6b138990d2ee9c3\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.41:9096\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.41:9096 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.41:9096 上的微服务已停止服务\"},\"startsAt\":\"2020-05-14T09:44:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"42a3d283927754bd\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.41:9099\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.41:9099 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.41:9099 上的微服务已停止服务\"},\"startsAt\":\"2020-05-14T10:03:11.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"e3c8a92d5c29830a\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.6:9001\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.6:9001 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.6:9001 上的微服务已停止服务\"},\"startsAt\":\"2020-04-27T10:53:41.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"20c7e1e37eeeaa8e\"},{\"status\":\"firing\",\"labels\":{\"alertname\":\"eventcode-9920\",\"instance\":\"10.9.25.6:9005\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"annotations\":{\"description\":\"10.9.25.6:9005 consul-eureka-cluster 上的微服务已停止服务1分钟,请检查\",\"summary\":\"10.9.25.6:9005 上的微服务已停止服务\"},\"startsAt\":\"2020-04-30T13:47:56.299334014+08:00\",\"endsAt\":\"0001-01-01T00:00:00Z\",\"generatorURL\":\"http://10.9.25.19/prometheus/graph?g0.expr=up%7Bjob%3D~%22spring-boot.%2A%7Cconsul-eureka.%2A%22%7D+%3D%3D+0\\u0026g0.tab=1\",\"fingerprint\":\"db86c0bbed38d30a\"}],\"groupLabels\":{\"alertname\":\"eventcode-9920\"},\"commonLabels\":{\"alertname\":\"eventcode-9920\",\"job\":\"consul-eureka-cluster\",\"service\":\"spring-boot-exporter\",\"severity\":\"warning\"},\"commonAnnotations\":{},\"externalURL\":\"http://monitor-test:9093\",\"version\":\"4\",\"groupKey\":\"{}/{alertname=~\\\"^(?:eventcode-.*)$\\\"}:{alertname=\\\"eventcode-9920\\\"}\"}";
JSONObject jsonObject = JSONObject.parseObject(str, JSONObject.class);
String jsonString = JSON.toJSONString(jsonObject, SerializerFeature.MapSortField);
System.out.println(jsonString);
其中最重要的是
String jsonString = JSON.toJSONString(jsonObject, SerializerFeature.MapSortField);
这个是让字符串排序的
其他内容:
LinkedHashMap<String, Object> jsonMap = JSON.parseObject(str,LinkedHashMap.class, Feature.OrderedField);
对于上个代码的 Feature.OrderedField这个是保持原有字符串的顺序。