Mysql-如何从json提交的价值

时间:2016-05-07 23:28:20

标签: mysql json regex

我有一个列,其中表单数据放在json字符串中,我如何使用查询提取数据。我已经通过互联网检查了解决方案,每个人建议应用正则表达式。我如何应用正则表达式从字符串下面获取Pass Type的值。

.....{\"label\":\"Pass Type\",\"value\":\"prof_3Day\",\"identifier\":\"field102\",\"type\":\"dropdown\",\"page\":3,\"page_name\":\"Step 3\",\"options\":[{......

1 个答案:

答案 0 :(得分:1)

转发

由于所有可能的边缘情况,我不建议使用正则表达式来搜索JSON字符串。但是,由于听起来您对JSON字符串有创造性的控制权,因此您应该能够避免许多会导致问题的边缘情况。

描述

此正则表达式将执行以下操作:

  • 搜索密钥label并返回其值
  • 从值
  • 中删除引号
  • 在处理JSON字符串时避免明显的边缘情况
  • 我不确定你的示例文本是否真的有\"个子字符串,因此这个正则表达式在有或没有\
  • 的情况下编写得很好

正则表达式:

\{(?:"[^"]*"|[^{}"]*|\{[^{}]*})*?"label\\?":\K"([^"]*)\\?"

Regular expression visualization

实施例

来源文字

.....{\"label\":\"Pass Type\",\"value\":\"prof_3Day\",\"identifier\":\"field102\",\"type\":\"dropdown\",\"page\":3,\"page_name\":\"Step 3\",\"options\":[{......

生成的捕获组

  • 组0从原始字符串
  • 获取label和值集
  • 第1组只获取值,并删除斜线和引号

样本匹配

[0] => \"Pass Type\"
[1] => Pass Type

解释

  • 导致有助于避免许多凌乱的边缘案例
  • 可能不支持\K,因此您可以删除它。如果它被删除,那么捕获组0将包含直到label keyname
  • 的整个字符串

<强>详情

NODE                     EXPLANATION
----------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the least amount possible)):
----------------------------------------------------------------------
    [,:]                     any character of: ',', ':'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    "                        '"'
----------------------------------------------------------------------
    [^"]*                    any character except: '"' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    "                        '"'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    [^{}"]*                  any character except: '{', '}', '"' (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \{                       '{'
----------------------------------------------------------------------
    [^{}]*                   any character except: '{', '}' (0 or
                             more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    }                        '}'
----------------------------------------------------------------------
  )*?                      end of grouping
----------------------------------------------------------------------
  \K                       'K'
----------------------------------------------------------------------
  \\?                      '\' (optional (matching the most amount
                           possible))
----------------------------------------------------------------------
  "label                   '"label'
----------------------------------------------------------------------
  \\?                      '\' (optional (matching the most amount
                           possible))
----------------------------------------------------------------------
  ":                       '":'
----------------------------------------------------------------------
  \\?                      '\' (optional (matching the most amount
                           possible))
----------------------------------------------------------------------
  "                        '"'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^"]*?                   any character except: '"' (0 or more
                             times (matching the least amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \\?                      '\' (optional (matching the most amount
                           possible))
----------------------------------------------------------------------
  "                        '"'