2014-09-29 21 views
1

https://github.com/interagent/http-api-design#downcase-paths-and-attributes優勢snake_case在駝峯在JSON的REST

Downcase attributes as well, but use underscore separators so that attribute names can be typed without quotes in JavaScript, e.g.:

service_class: "first"

採取什麼它的「屬性名稱可以不帶引號鍵入」是什麼意思?

+1

你已經用'json'標記了這個問題,所以我想指出的是,如果你使用JSON,你的名字 - 值對中的名字必須總是被引用。這是有效JSON的要求(http://stackoverflow.com/a/2068074/1281907)。 – talemyn 2014-09-29 18:33:46

回答

1

這是一個有效的JavaScript文字:

{ foo_bar: 'baz' } 

這不是:

{ foo-bar: 'baz' } 

後者必須是:

{ 'foo-bar': 'baz' } 

這一切它正在談論;前面的段落提到了「破折號分隔的路徑名稱」,接着說你不應該在JSON中使用破折號。這不是蛇與駝峯,而是蛇與「破案」。

1

如果您有:

{ foo-bar: 1 } 

你會得到一個 「語法錯誤:意外標記:」 錯誤,因爲 「 - 」 是誤認爲是減法運算符:

{ foo - bar : 1 } 

然後,你需要引號以表明這是一個屬性名稱:

{ "foo-bar": 1 } 

如果使用下劃線而不是破折號,毫不含糊:

{ "foo_bar": 1 } 
{ foo_bar: 1 } // This also works