日期:2014-05-16  浏览次数:20468 次

对json进行注释
今天看到老外http://fadefade.com/json-comments.html中,谈到了对json文件的注释,
感觉虽然不常用,但還是值得关注下,其实原理很简单,比如:

> Object.keys(JSON.parse('{"a": 1, "a": 2}')).length;
=> 1;

  如果key相同,取的是后一个,因此可以用这个方法,如:
{
  "api_host" : "The hostname of your API server. You may also specify the port.",
  "api_host" : "hodorhodor.com",

  "retry_interval" : "The interval in seconds between retrying failed API calls",
  "retry_interval" : 10,

  "auth_token" : "The authentication token. It is available in your developer dashboard under 'Settings'",
  "auth_token" : "5ad0eb93697215bc0d48a7b69aa6fb8b",

  "favorite_numbers": "An array containing my all-time favorite numbers",
  "favorite_numbers": [19, 13, 53]
}


第一个就是注释了,其实json解析器会把上面的json解析为
{
    "api_host": "hodorhodor.com",
    "retry_interval": 10,
    "auth_token": "5ad0eb93697215bc0d48a7b69aa6fb8b",
    "favorite_numbers": [19,13,53]
}