Json 双引号搞砸了 C#

Json double quote messed up C#

伙计们,我很头疼尝试使用 C# 将 as3 文件序列化为 json。

现在我偶然发现了这个=>

"licvarreelVideosConfig":[{
    url: "ChoiceSlot2/GEOLJSlot/videos/00.flv",
    width: 224,
    height: 224,
    onWholeReel: false,
    transparent: true
}, {
    url:"ChoiceSlot2/GEOLJSlot/videos/01.flv",
    width: 224,
    height: 224,
    onWholeReel: false,
    transparent: true
}]

假设我根据 as3 文件中给出的内容生成 json 密钥。

但在某些 类 中,键中缺少双引号。 有什么简单的方法可以正确添加它们吗?

提前致谢

  1. 如果属性没有被引用,那你就不能真正调用这个JSON.

  2. 根据此site,在所有标准中,除了 RFC 7159 之外,所有内容都必须包含在 { }

  3. 抛开这些,我想到的一个快速解决方案是使用正则表达式将未引用的 属性 名称替换为引用的名称。

示例

var unquotedJson = "\"licvarreelVideosConfig\":[{" +
                        "url: \"ChoiceSlot2/GEOLJSlot/videos/00.flv\"," +
                        "width: 224," +
                        "height: 224," +
                        "onWholeReel: false," +
                        "transparent: true" +
                    "}, {" +
                        "url:\"ChoiceSlot2/GEOLJSlot/videos/01.flv\"," +
                        "width: 224," +
                        "height: 224," +
                        "onWholeReel: false," +
                        "transparent: true" +
                    "}]";

var quotedJson = new Regex("([a-zA-Z0-9_$]+?):(.*?)[,]{0,1}").Replace(unquotedJson, "\"\":");

// if the serializer needs nested { ... }
// var nestedQuotedJson = string.Format("{{{0}}}", quotedJson);

// do the serialization

注意,这确实不全面,它只支持属性个名称,其中包含a-z、A-Z、0-9、$和_字符。