JSON/ja

From Lazarus wiki
Revision as of 04:05, 28 May 2016 by Miyatakejiro (talk | contribs)
Jump to navigationJump to search

English (en) suomi (fi) 日本語 (ja) 한국어 (ko) polski (pl) русский (ru) 中文(中国大陆)‎ (zh_CN)

日本語版メニュー
メインページ - Lazarus Documentation日本語版 - 翻訳ノート - 日本語障害情報

概要

JSON (JavaScript Object Notation) は、簡単なテキストファイルを用いたデータ交換用の保存形式です。 JSONは、名前から分かるように JavaScript のオブジェクト表記法に基づいていますが JavaScript 専用ではありません。 人間による読み書きも簡単にでき、機械(パソコン)にとってもパースや生成が容易な形式です。

XMLと比較しても、より人間にとって読みやすい形式になっています。

JSON オブジェクト

JSON オブジェクトは、カンマ(,)で区切られた name/value の一対(メンバと呼びます)を、波括弧(なみかっこ; curly bracket: {})で括った集まりです。:

{"name1":value1, "name2":value2 ...}

上記は、可読性を高めるために、しばしば縦に name/value を表記するときもあります。:

{
	"name1": value1,
	"name2": value2
}

name は、2重引用符(ダブルクォート; double quote: "")に囲まれた文字列の形をとります。対して、valueは以下に示す表記が可能です。:

  • 単純な文字列(2重引用符で囲まれます)、数値、ブーリアン型、またはnull値:
    {"id":1, "name":"John Doe", "married":false}
  • 配列; 角カッコ(大カッコ; square bracket; [])に囲まれた、カンマ区切りの値の集まりです。:
    {"primeNumbers":[2, 3, 5, 7, 11, 13, 17], "oddNumbers":[1,3,5,7]}
  • オブジェクト; 波カッコ(中カッコ; curly bracket; {})に囲まれた、name/valueの一対の集まりです。:
    {"address":{"street":"145 Koinange Street", "City":"Nairobi", "Country":"Kenya"}}


JSON オブジェクトは、さらに複雑なオブジェクトを表現するために、どんどんネストする(入れ子にする)ことができます。:

{"user":
	{	"userid": 1900,
		"username": "jsmith",
		"password": "secret",
		"groups": [ "admins", "users", "maintainers"]
	}
}


Valid JSON format

There are a few rules and guidelines that define the JSON syntax (see RFC 4627):

  • JSON objects are encapsulated within opening and closing brackets { }. An empty object can be represented by { }
  • Arrays are encapsulated within opening and closing square brackets [ ]. An empty array can be represented by [ ]
  • A member is represented by a key-value pair
  • The key of a member should be contained in double quotes
  • Each member SHOULD have a unique key within an object structure
  • The value of a member must be contained in double quotes if it is a string
  • Boolean values are represented using the true or false literals in lower case
  • Number values are represented using double-precision floating-point format; Scientific notation is supported; Numbers should not have leading zeroes
  • "Offensive" characters in a string need to be escaped using the backslash character
  • Null values are represented by the null literal in lower case
  • Other object types, such as dates, are not natively supported and should be converted to strings; to be managed by the parser/client
  • Each member of an object or each array value must be followed by a comma if it is not the last one
  • The common extension for json files is .json
  • The MIME type for json files is application/json

Implementations

JSON implementation is not very strict, and a lot of leeway is granted to the client application and/or parser to enforce the guidelines. As usual when writing code:

  • be liberal in what you accept
  • be strict (adhering to the standard) in what you send.

There are two main implementations of JSON:

The official JSON Specification syntax

This implementation adheres strictly to the RFC 4627 guidelines above and does not allow deviation from the specification.

The Javascript syntax

This implementation follows the implementation of the Javascript programming language and as such allows for a few deviations from the official specification. For example:

  1. allows un-quoted keys eg {name: "John Doe" }
  2. allows single quotes for keys and/or string values; and a liberal mix of single and double quotes eg {'name': "John Doe", "language":'Pascal'}
  3. allows trailing comma after the last member of an array and/or object eg {"keywords":["if","begin","for",], "IDEs":["Lazarus","fpIDE","MSEide"],}

See also

  • fcl-json FreePascal と Lazarus に JSON を実装しているパッケージ
  • 公式の JSON webサイト
  • Streaming JSON/de - (この記事の、オリジナルのドイツ語記事)
  • Streaming JSON - (オリジナルの英訳の記事だが、記事頭に、Google翻訳による翻訳であり不正確な英語や間違っているデータがあるため修正が必要であり、他の方は機械翻訳を使わないように、との記述がある。よって、あまり参考にならなかった。)
  • Streaming JSON/ja - (上記、英訳の記事を参考に、現在(20150525時点)で、WINDOWS8.1、64bit上での実行を確認したソースに差し替え、さらに実用的な例を追加してみました。)
  • 上記英訳の記事では、ロシア語の記事へのリンクもあったが、それもドイツ語記事の翻訳との記述があったため、削除した。読める方は、英語の記事のリンクから読んでください。