【问题标题】:How to put double quotes for every array element in json with perl如何使用perl为json中的每个数组元素加上双引号
【发布时间】:2018-08-11 19:26:55
【问题描述】:

请帮助我如何为以下 JSOn 的每个元素添加双引号:

$new_constraint='{categoryIds:[0c5401a4-7d96-44f8-a229-7403453512e8],enforcedAt:'.$availableAtVar.',enforcedUntil:'.$availableUntilVar.'}';

最后应该如下图所示:

{"categoryIds":["0c5401a4-7d96-44f8-a229-7403453512e8"],"enforcedAt":"2007-22-08.00:00:000","enforcedUntil":"2007-22-08.00:00:000"}

【问题讨论】:

  • 但这不是有效的 JSON。你能修复生成损坏 JSON 的代码吗?

标签: arrays json perl


【解决方案1】:

为什么不首先生成有效的 JSON?

例如

use JSON::MaybeXS;
...
my $new_constraint = encode_json({
    categoryIds   => ['0c5401a4-7d96-44f8-a229-7403453512e8'],
    enforcedAt    => $availableAtVar,
    enforcedUntil => $availableUntilVar,
});

这样您就没有需要稍后修复的“几乎是 JSON”。

【讨论】:

    猜你喜欢
    • 2013-09-26
    • 1970-01-01
    • 2010-10-18
    • 2021-02-20
    • 2011-05-02
    • 1970-01-01
    • 2015-05-28
    • 2011-10-08
    • 2015-10-25
    相关资源
    最近更新 更多