简体   繁体   中英

Json double quote messed up C#

Guys I have a big headaches trying to serialize as3 file to json with C#.

Right now i stumbled on with this =>

"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
}]

Lets say I generate the json keys based on what is given from the as3 file.

But in some of the classes there are missing double quotes in the keys. Any easy way to properly add them ?

Thanks in advance

  1. If the properties are not quoted, then you can't really call this JSON.

  2. According to this site , in all the standards, except for RFC 7159, the whole content has to be wrapped in { }

  3. Putting aside these, a quick solution that comes to my mind involves using a regular expression to replace the unquoted property names with quoted ones.

Example

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, "\"$1\":$2");

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

// do the serialization

Note, this is really not comprehensive, it only supports property names with az, AZ, 0-9, $ and _ characters in them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM