【问题标题】:Apply regex expression to a json response data将正则表达式应用于 json 响应数据
【发布时间】:2016-11-18 19:49:44
【问题描述】:

场景客户端发出 GET 请求,响应是这样的 JSON 格式

     var data = {
    "enabled": true,
    "state": "schedule",
    "schedules": [
        {
        "rule": {
        "start": "2014-06-29T12:36:26.000",
        "end": "2014-06-29T12:36:56.000",
        "recurrence": [
        "RRULE:FREQ=MINUTELY"
        ]
        },
        "wifi_state_during_rule": "disabled",
        "end_state": "enabled"
        }
    ],
    "calculated_wifi_state_now": "disabled",
    "time_of_next_state_change": [
        "2014-07-08T18:56:56.000Z",
        "2014-07-08T18:57:56.000Z"
    ]
};

出于本示例的目的,我将结果存储在一个名为“data”的变量中。 我的正则表达式是:

checkPattern = /"\w+\"(?=:)/ //all keys "keyname": ...

这里的基本想法只是为了获取键名,而不是在对象或数组中......因为 JSON 中键名的定义是“键名”:这就是我尝试使用上述正则表达式的原因。

我什至想过用递归函数来做这件事,但没用。

【问题讨论】:

  • 为什么要使用正则表达式? Json 字符串可以很容易地转换为 javascript、c#、Java 和许多其他语言中的实际对象。真的没有必要使用正则表达式。因此,请详细说明对正则表达式的需求或进行研究以将其转化为对象。信息就在那里。
  • 这里的重点不是使用正则表达式。我想只提取名称,即使它们是嵌套的。我使用正则表达式进行测试

标签: javascript json regex


【解决方案1】:

您永远不应该使用正则表达式解析非常规结构。

只需从已解析的 json 对象中收集您想要的内容。
只需运行 data = JSON.parse(json_string) 即可对其进行解析

function getKeysRecursive(obj) {
  var result = [];
  for (var key in obj) {
    result.push(key);
    if (typeof obj[key] == 'object') {
       result = result.concat(getKeysRecursive(obj[key]));
    }
  }
  return result;
}

getKeysRecursive(({
    "enabled": true,
    "state": "schedule",
    "schedules": [
        {
        "rule": {
        "start": "2014-06-29T12:36:26.000",
        "end": "2014-06-29T12:36:56.000",
        "recurrence": [
        "RRULE:FREQ=MINUTELY"
        ]
        },
        "wifi_state_during_rule": "disabled",
        "end_state": "enabled"
        }
    ],
    "calculated_wifi_state_now": "disabled",
    "time_of_next_state_change": [
        "2014-07-08T18:56:56.000Z",
        "2014-07-08T18:57:56.000Z"
    ]
}))

// ["enabled", "state", "schedules", "0", "rule", "start", "end", "recurrence", "0", "wifi_state_during_rule", "end_state", "calculated_wifi_state_now", "time_of_next_state_change", "0", "1"]

您可以过滤、排序、排除数字键...所有您需要的。

【讨论】:

  • 谢谢先生,这正是我真正需要的。
【解决方案2】:

您不需要为此使用正则表达式。 Javascript 有一个内置函数来提取对象键名。

示例:

使用 Object.keys();

 var data = {
    "enabled": true,
    "state": "schedule",
    "schedules": [
        {
        "rule": {
        "start": "2014-06-29T12:36:26.000",
        "end": "2014-06-29T12:36:56.000",
        "recurrence": [
        "RRULE:FREQ=MINUTELY"
        ]
        },
        "wifi_state_during_rule": "disabled",
        "end_state": "enabled"
        }
    ],
    "calculated_wifi_state_now": "disabled",
    "time_of_next_state_change": [
        "2014-07-08T18:56:56.000Z",
        "2014-07-08T18:57:56.000Z"
    ]
};

那么

console.log(Object.keys(data));

应该打印

["enabled","state","schedules","calculated_wifi_state_now","time_of_next_state_change"]

证明:http://codepen.io/theConstructor/pen/zBpWak

现在您所有的对象键都存储在一个数组中..

希望对你有帮助

【讨论】:

    【解决方案3】:

    这将为您提供所有 keys 作为匹配项:

    \"(\w+)(?:\"\:)

    https://regex101.com/r/iM9wB3/2

    编辑为在一行中使用多个keys

    【讨论】:

    • 如果一行中有多个键,这不起作用。 + 太贪心了。
    猜你喜欢
    • 2021-12-06
    • 2019-08-01
    • 2014-10-07
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 2016-06-29
    相关资源
    最近更新 更多