开发者

is trailing comma and/or non existant key ok in javascript object notation?

I made a little code gen and was wondering if I should go to the trouble of handling the extra comma at the end. IE seems to ignore it, but I need to keep it cross browser, and I'd like to generate valid code anyway.

function init() {
var myOptions = { : 'Select home value', // <== THERE'S THE NON EXISTANT KEY
100000 : '$90,001 - $100,000',
1000000 : '$950,001 - $1,000,000',
1000001 : 'Over $1,000,000', // <== HERE'S THE COMMA I'M CURIOUS ABOUT
};

here's the code to generate

protected string DoTransform()
{
    var sb = new StringBuilder("var myOptions = {");
    foreach (var option in 
        XDocument.L开发者_如何转开发oad(MapPath("~/App_Data/Data.xml"))
            .XPathSelectElements("./data/options[@question='ApproximatePropertyValue']/option"))
    {
        sb.AppendFormat("{0} : '{1}',\n", option.Attribute("value").Value, option.Value);
    }
    sb.AppendLine("};");
    return sb.ToString();
}

ANSWER: Here's the updated code that takes care of the empty key (by skipping the first element) and trailing comma (by rearranging logic so TrimEnd can nab it).

protected string DoTransform()
{
    var sb = new StringBuilder();
    foreach (var option in 
        XDocument.Load(MapPath("~/App_Data/Data.xml"))
            .XPathSelectElements("./data/options[@question='ApproximatePropertyValue']/option")
            .Skip(1))
    {
        sb.AppendFormat("{0}:'{1}',", option.Attribute("value").Value, option.Value);
    }
    return"var myOptions = {\n" + sb.ToString().TrimEnd(',') + "};";
}


My understanding is that most browsers will allow the trailing comma, but it is not something which is acceptable in the JSON spec, so it is a bad idea. On the other hand, the fact that you are missing a key in that first key-value pair won't be forgiven... by ANYONE ;-)

Edit

Just saw your code above. Forgive me, my .NET is rusty (as in, I've barely looked at it, ever), but I believe this will work:

foreach (var option in 
    XDocument.Load(MapPath("~/App_Data/Data.xml"))
       .XPathSelectElements(
             "./data/options[@question='ApproximatePropertyValue']/option"
       )
    )
{
    // use the length as a flag, if you've added to it, it will be longer than 16
    if(sb.Length > 20)sb.Append(",");
    sb.AppendFormat("\n{0} : '{1}'", 
        option.Attribute("value").Value, option.Value);
}


Opera 12 says:

[14.07.2011 03:44:53] JavaScript - ...
Inline script compilation
Syntax error at line 6 while loading:
  var myOptions = {: 'Select home value
-------------------^
expected '}', got ':'

You should better look for a library here: http://json.org/ instead of implementing the wheel again.

(Please don't tell anyone that I gave you that awful hacky advice:) You could append a sentinel like "_ignore_me": false to the sb.


At least IE7 and older versions will have problems with the trailing comma. In newer versions it depends on the document-mode.


IE doesn't like the trailing commas. Other browsers are Ok.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜