【问题标题】:Check wether a JSON is sorted in Java检查 JSON 是否在 Java 中排序
【发布时间】:2020-09-29 06:14:27
【问题描述】:

您好,我有一个 json 文件,我正在尝试检查正在读取的数据是否已排序。所以我可以根据是否返回真或假。不幸的是,我不知道如何解决这个问题,因此我们将不胜感激。

代码:

public void Trial () throws JSONException {
        String json = "[{"appId": "self","askPrice": 0,"bidPrice": 0,"collectionDataSource": "ExternalTick","collectionName": "data","collectionObservationTime": "2020-09-21T17:47:59.703Z","collectionType": "LIVE","coupon": 1.03,"createdBy": "Test","createdOn": "2020-09-21T17:47:59.703Z","createdOnDate": 0,"forward": 0,"issuingAgency": "FN15","lastUpdated": "2020-09-21T17:47:59.703Z","lastUpdatedBy": "string","lastUpdatedDate": 0,"maturity": ,"midPrice":0 ,"mtaVersionNumber": 0,"settlementDate": "2020-09-21T17:47:59.703Z"},{"appId": "bob","askPrice": 0,"bidPrice": 0,"collectionDataSource": "ExternalTick","collectionName": "data","collectionObservationTime": "2020-09-21T17:47:59.703Z","collectionType": "LIVE","coupon": 1.03,"createdBy": "Test","createdOn": "2020-09-21T17:47:59.703Z","createdOnDate": 0,"forward": 0,"issuingAgency": "GN30","lastUpdated": "2020-09-21T17:47:59.703Z","lastUpdatedBy": "string","lastUpdatedDate": 0,"maturity": ,"midPrice":0 ,"mtaVersionNumber": 0,"settlementDate": "2020-09-21T17:47:59.703Z"},{"appId": "Mnd","askPrice": 0,"bidPrice": 0,"collectionDataSource": "ExternalTick","collectionName": "data","collectionObservationTime": "2020-09-21T17:47:59.703Z","collectionType": "LIVE","coupon": 1.03,"createdBy": "Test","createdOn": "2020-09-21T17:47:59.703Z","createdOnDate": 0,"forward": 0,"issuingAgency": "FN30","lastUpdated": "2020-09-21T17:47:59.703Z","lastUpdatedBy": "string","lastUpdatedDate": 0,"maturity": ,"midPrice":0 ,"mtaVersionNumber": 0,"settlementDate": "2020-09-21T17:47:59.703Z"}]";
        JSONArray jsonObj = new JSONArray(json);
        for (int i = 0; i < jsonObj.length(); i++) {
            JSONObject jsonobject = jsonObj.getJSONObject(i);
            String Coupon = jsonobject.getString("issuingAgency");
            // logic to see if code is sorted or not 
        }
    }

试图查看 issuingAgency 是否按以下顺序排序:

FN15 - FN30 - GN30

JSON:

[
  {
    "appId": "self",
    "askPrice": 0,
    "bidPrice": 0,
    "collectionDataSource": "ExternalTick",
    "collectionName": "data",
    "collectionObservationTime": "2020-09-21T17:47:59.703Z",
    "collectionType": "LIVE",
    "coupon": 1.03,
    "createdBy": "Test",
    "createdOn": "2020-09-21T17:47:59.703Z",
    "createdOnDate": 0,
    "forward": 0,
    "issuingAgency": "FN15",
    "lastUpdated": "2020-09-21T17:47:59.703Z",
    "lastUpdatedBy": "string",
    "lastUpdatedDate": 0,
    "maturity": ,
    "midPrice":0 ,
    "mtaVersionNumber": 0,
    "settlementDate": "2020-09-21T17:47:59.703Z"
  },
  {
    "appId": "bob",
    "askPrice": 0,
    "bidPrice": 0,
    "collectionDataSource": "ExternalTick",
    "collectionName": "data",
    "collectionObservationTime": "2020-09-21T17:47:59.703Z",
    "collectionType": "LIVE",
    "coupon": 1.03,
    "createdBy": "Test",
    "createdOn": "2020-09-21T17:47:59.703Z",
    "createdOnDate": 0,
    "forward": 0,
    "issuingAgency": "GN30",
    "lastUpdated": "2020-09-21T17:47:59.703Z",
    "lastUpdatedBy": "string",
    "lastUpdatedDate": 0,
    "maturity": ,
    "midPrice":0 ,
    "mtaVersionNumber": 0,
    "settlementDate": "2020-09-21T17:47:59.703Z"
  },
  {
    "appId": "Mnd",
    "askPrice": 0,
    "bidPrice": 0,
    "collectionDataSource": "ExternalTick",
    "collectionName": "data",
    "collectionObservationTime": "2020-09-21T17:47:59.703Z",
    "collectionType": "LIVE",
    "coupon": 1.03,
    "createdBy": "Test",
    "createdOn": "2020-09-21T17:47:59.703Z",
    "createdOnDate": 0,
    "forward": 0,
    "issuingAgency": "FN30",
    "lastUpdated": "2020-09-21T17:47:59.703Z",
    "lastUpdatedBy": "string",
    "lastUpdatedDate": 0,
    "maturity": ,
    "midPrice":0 ,
    "mtaVersionNumber": 0,
    "settlementDate": "2020-09-21T17:47:59.703Z"
  }
]

在给定的 JSON 中,代码将返回 false,因为 issueAgency 未排序。 感谢您的帮助。

【问题讨论】:

  • 跟踪前一个ID,检查当前ID是否按字典顺序大于前一个ID。如果不返回 false。

标签: java arrays json sorting


【解决方案1】:

您所要做的就是比较当前issuingAgency 是否小于前一个:


public void Trial () throws JSONException {
        String json = "[{"appId": "self","askPrice": 0,"bidPrice": 0,"collectionDataSource": "ExternalTick","collectionName": "data","collectionObservationTime": "2020-09-21T17:47:59.703Z","collectionType": "LIVE","coupon": 1.03,"createdBy": "Test","createdOn": "2020-09-21T17:47:59.703Z","createdOnDate": 0,"forward": 0,"issuingAgency": "FN15","lastUpdated": "2020-09-21T17:47:59.703Z","lastUpdatedBy": "string","lastUpdatedDate": 0,"maturity": ,"midPrice":0 ,"mtaVersionNumber": 0,"settlementDate": "2020-09-21T17:47:59.703Z"},{"appId": "bob","askPrice": 0,"bidPrice": 0,"collectionDataSource": "ExternalTick","collectionName": "data","collectionObservationTime": "2020-09-21T17:47:59.703Z","collectionType": "LIVE","coupon": 1.03,"createdBy": "Test","createdOn": "2020-09-21T17:47:59.703Z","createdOnDate": 0,"forward": 0,"issuingAgency": "GN30","lastUpdated": "2020-09-21T17:47:59.703Z","lastUpdatedBy": "string","lastUpdatedDate": 0,"maturity": ,"midPrice":0 ,"mtaVersionNumber": 0,"settlementDate": "2020-09-21T17:47:59.703Z"},{"appId": "Mnd","askPrice": 0,"bidPrice": 0,"collectionDataSource": "ExternalTick","collectionName": "data","collectionObservationTime": "2020-09-21T17:47:59.703Z","collectionType": "LIVE","coupon": 1.03,"createdBy": "Test","createdOn": "2020-09-21T17:47:59.703Z","createdOnDate": 0,"forward": 0,"issuingAgency": "FN30","lastUpdated": "2020-09-21T17:47:59.703Z","lastUpdatedBy": "string","lastUpdatedDate": 0,"maturity": ,"midPrice":0 ,"mtaVersionNumber": 0,"settlementDate": "2020-09-21T17:47:59.703Z"}]";
        JSONArray jsonObj = new JSONArray(json);
 
        boolean isOrdered = true;

        String lastCoupon = jsonObj.getJSONObject(0).getString("issuingAgency");
        for (int i = 1; i < jsonObj.length(); i++) {
            JSONObject jsonobject = jsonObj.getJSONObject(i);
            String Coupon = jsonobject.getString("issuingAgency");
            // logic to see if code is sorted or not 
            if(Coupon.compareTo(lastCoupon) < 0){
               isOrdered = false;
               break;
            }
            lastCoupon = Coupon;
        }
        System.out.println("if ordered = " + isOrdered);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2015-06-07
    • 2017-11-20
    • 2012-04-08
    • 1970-01-01
    相关资源
    最近更新 更多