繁体   English   中英

根据路径和正则表达式过滤JSON

[英]Filter JSON based of path and regex

我希望能够让用户能够指定路径和有效的正则表达式,并基于此返回过滤的JSON。

我想除了我不知道如何动态获取路径结果之外,几乎已经有了解决方案。 这是我的代码:

function getResult(jsonObject, pathText, regexToCheck) {
    var pathArr = pathText.split(".")
    var jsonPath = ''
    console.log('start...')
    for(var key in pathArr){
        var addPath = "['"+ pathArr[key] +"']"
        jsonPath += addPath
    }
    result = jsonObject[jsonPath]
    return result.match(new RegExp(regexToCheck, 'g'), match)
}

function filterBy (json, path, regexToCheck){
    var parseJSON = json
    var filterResult = [];
    for(var obj in parseJSON){
        var result = getResult(parseJSON[obj], path, regexToCheck)
        console.log(result)
        if (result == true){
            filteredResult.push(parseJSON[obj])
        }
    }
    return filterResult
}

filterBy(json, path, regexToCheck)

我想前面提到的是让用户指定一个路径和正则表达式,例如var path = 'configurationId.id'var regexToCheck = /^[4]/并给出以下测试数据

var json = [{
    "configurationId": {
      "id": "7000",
      "displayName": "7000",
      "uri": "/configuration/users/7000"
    },
    "licenseProperties": {
      "hasClientAccess": true
    },
    "roles": {
      "actualValue": [{
          "id": "Agent",
          "displayName": "Agent",
          "uri": "/configuration/roles/Agent"
        },
        {
          "id": "SMS",
          "displayName": "SMS",
          "uri": "/configuration/roles/SMS"
        }
      ]
    }
  }, {
    "configurationId": {
      "id": "7001",
      "displayName": "7001",
      "uri": "/configuration/users/7001"
    },
    "licenseProperties": {
      "hasClientAccess": true
    },
    "roles": {
      "actualValue": [{
          "id": "Agent",
          "displayName": "Agent",
          "uri": "/configuration/roles/Agent"
        },
        {
          "id": "SMS",
          "displayName": "SMS",
          "uri": "/configuration/roles/SMS"
        }
      ]
    }
  }, {
    "configurationId": {
      "id": "7002",
      "displayName": "7002",
      "uri": "/configuration/users/7002"
    },
    "licenseProperties": {
      "hasClientAccess": true
    },
    "roles": {
      "actualValue": [{
          "id": "Agent",
          "displayName": "Agent",
          "uri": "/configuration/roles/Agent"
        },
        {
          "id": "SMS",
          "displayName": "SMS",
          "uri": "/configuration/roles/SMS"
        }
      ]
    }
  }, {
    "configurationId": {
      "id": "4003",
      "displayName": "4003",
      "uri": "/configuration/users/4003"
    },
    "licenseProperties": {
      "hasClientAccess": true
    },
    "roles": {
      "actualValue": [{
          "id": "Agent",
          "displayName": "Agent",
          "uri": "/configuration/roles/Agent"
        },
        {
          "id": "SMS",
          "displayName": "SMS",
          "uri": "/configuration/roles/SMS"
        }
      ]
    }
  }];

让结果返回,

{
    "configurationId": {
      "id": "4003",
      "displayName": "4003",
      "uri": "/configuration/users/4003"
    },
    "licenseProperties": {
      "hasClientAccess": true
    },
    "roles": {
      "actualValue": [{
          "id": "Agent",
          "displayName": "Agent",
          "uri": "/configuration/roles/Agent"
        },
        {
          "id": "SMS",
          "displayName": "SMS",
          "uri": "/configuration/roles/SMS"
        }
      ]
    }

由于我的正则表达式只是检查id是否以4开头。因此,还必须注意,它必须与嵌套的JSON一起使用,因此必须指定路径规范。 因此,要重申我的代码失败的地方基本上是在这里: result = jsonObject[jsonPath]

好吧,所以它最终变成了一个循环,您在该循环中基于先前提供的数组沿路径行进。 这是我的完整解决方案,并且我修复了一个错误,无论您何时要遍历对象内部的数组

export function filterBy (json, path, regexToCheck) {
  var parseJSON = json
  var filteredResult = []
  for (var obj in parseJSON) {
    var result = getResult(parseJSON[obj], path, regexToCheck)
    if (result == true) {
      filteredResult.push(parseJSON[obj])
    }
  }
  return filteredResult
}

function getResult (jsonObject, pathText, regexToCheck) {
  var pathArr = pathText.split('.')
  var testObj
  var testResult
  var foundmatch = false
  for (var item in pathArr) {
    // Handles Arrays
    if (Object.prototype.toString.call(jsonObject) == '[object Array]') {
      jsonObject.forEach(function (obj) {
        testObj = ''
        testResult = ''
        testObj = obj[pathArr[item]]
        testResult = testObj.match(new RegExp(regexToCheck, 'g'))
        if (testResult != null) foundmatch = true
      })
    } else {
      testResult = ''
      jsonObject = jsonObject[pathArr[item]]
    }
  }

  if (Object.prototype.toString.call(jsonObject) != '[object Array]') {
    jsonObject = jsonObject.match(new RegExp(regexToCheck, 'g'))
    if (jsonObject != null) foundmatch = true
  }

  return foundmatch
}

暂无
暂无

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

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