Yordan Kanchelov
Yordan Kanchelov

Reputation: 581

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

Upvotes: 1

Views: 1361

Answers (1)

Szabolcs Dézsi
Szabolcs Dézsi

Reputation: 8843

  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 a-z, A-Z, 0-9, $ and _ characters in them.

Upvotes: 1

Related Questions