【问题标题】:How to use regular expression in json(PHP)?如何在 json(PHP) 中使用正则表达式?
【发布时间】:2010-02-09 10:07:28
【问题描述】:

现在我有一个这样的 json 代码:

{"1":
    {
    "text":"e1",
    "children":
        {
        "1":
            {
            "text":"e1_site1",
            "children":
                {"1":"e1_site1_salarie1_nom"}
            },
        "3":
            {
            "text":"e1_site2",
            "children":
                {
                "3":"e1_site2_sa1",
                "4":"e1_site2_sa2"
                }
            }
        }
    },
"2":
    {
    "text":"e2",
    "children":
        {
        "2":
            {
            "text":"e2_site2",
            "children":
                {
                "2":"e2_site2_salarie2_nom"
                }
            }
        }
    }
}

我希望它是:

[
    {
    "text":"e1",
    "children":
    [
        {
        "text":"e1_site1",
        "children":
        [
            {
            "text":"e1_site1_salarie1_nom"
            }
        ],
        "text":
            {
            "text":"e1_site2",
            "children":
            [
                {
                "text":"e1_site2_sa1"
                },
                {
                "text":"e1_site2_sa2"
                }
            ]
            }
    ],
    "text":
        {
        "text":"e2",
        "children":
        [
            {
            "text":"e2_site2",
            "children":
            [
                {
                "text":"e2_site2_salarie2_nom"
                }
            ]
            }
        ]
        }

我现在所做的,还没有完成:

$json = json_encode($entreprise);
$mode = array("/{\"[0-9]\":{\"text\"/", "/children\":{\"[0-9]\"/","/,\"[0-9]\":{\"/", "/,\"[0-9]\":\"/","(}})");
$replacement = array("[{\"text\"","children\":[{\"text\"",",\"text\":{\"",",\"text\":\"","}]");
$json = preg_replace($mode, $replacement, $json);
dump($json);

以及目前的结果:

[
    {
    "text":"e1",
    "children":
    [
        {
        "text":"e1_site1",
        "children":
        [
            {
            "text":"e1_site1_salarie1_nom"
            }
        ],
        "text":
            {
            "text":"e1_site2",
            "children":
            [
                {
                "text":"e1_site2_sa1",
                "text":"e1_site2_sa2"
                }
            ]
            }
    ],
    "text":
        {
        "text":"e2",
        "children":
        [
            {
            "text":"e2_site2",
            "children":
            [
                {
                "text":"e2_site2_salarie2_nom"
                }
            ]
            }
        ]
        }

你知道如何用正则表达式达到理想的输出吗?

任何想法或建议将不胜感激!提前致谢。 ;)

编辑:

根据灵魂融合的建议更改了输出。是这样的:

[
    {
    "text":"e1",
    "children":
        {
        "1":
            {
            "text":"e1_site1",
            "children":
                {
                "1":"e1_site1_salarie1_nom"
                }
            },
        "3":
            {
            "text":"e1_site2",
            "children":
                {
                "3":"e1_site2_sa1",
                "4":"e1_site2_sa2"
                }
            }
        }
    },
    {
    "text":"e2",
    "children":
        {
        "2":
            {
            "text":"e2_site2",
            "children":
                {
                "2":"e2_site2_salarie2_nom"
                }
            }
        }
    }
]

但它距离我的理想结果还有很长的路要走。

有人知道怎么做吗?

【问题讨论】:

  • 某事告诉我这不是要走的路...
  • 我不明白,什么意思?还有其他方法吗?

标签: php regex json


【解决方案1】:

正则表达式是您可以在这里使用的最糟糕的方法:

  • 如果您在 3 个月后返回此代码,您将无法理解自己做了什么,
  • 很容易犯难以发现的错误,
  • 如果 PHP 决定更改其 json 生成代码,代码将中断

我建议在 $entreprise 数组上进行所有操作而不冒任何风险,然后然后在其上调用 json_encode()

生成的 JSON 有对象而不是数组的原因是原始数组不是 0 索引的。试试这个,例如:

dump(json_encode(array_values($enterprise)));

【讨论】:

  • @soulmerge,谢谢。好的,我现在明白你的意思了。我需要对数组进行操作,而不是对json进行操作?
  • 没错,更改数组比使用正则表达式更改 json 字符串要容易得多。
  • 是的,它正在使用 'dump(json_encode(array_values($enterprise)));'。但我认为我仍然需要对数组进行一些操作。谢谢。
  • 我认为,更改数组并不比更改 json 容易。太可怕了!
  • 我没有通过更改数组来做到这一点。但我已经使用我帖子中的方法完成了这项工作。
【解决方案2】:

JSON 不是正则语言,因此您不能使用正则表达式来解析它。并且您的预期输出不是有效的 JSON,因为同一键不能多次出现,如

{
    "text":"e1_site2_sa1",
    "text":"e1_site2_sa2"
}

【讨论】:

  • @Gumbo,对不起,我弄错了,我改了。您可以在帖子中看到它。
  • 次要挑剔:JSON 是一种将数据表示为字符串的方式,因此您可以使用正则表达式来解析/更改它。当然,这仍然是个坏主意 ;-)
  • @MSpreij:JSON not 正则,因此您不能使用正则表达式来解析它。只有特定部分是常规的(stringnumbertruefalsenull)但不是全部(object数组).
  • 啊..我想我误解了“常规”的含义。谢谢指正。
【解决方案3】:

您应该找到从 json_encode 输出所需 json 输出的方法,而不是通过输出您不想要的东西然后用正则表达式欺骗它们。 所以我的猜测是,工作是在你的数据结构上而不是在文本输出上...​​...

【讨论】:

  • @Philipe,是的,我认为你是对的。我会努力弄清楚的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 2013-07-08
  • 1970-01-01
  • 2013-05-27
  • 2016-03-29
相关资源
最近更新 更多