3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JSON入門

Last updated at Posted at 2019-03-16

JSON入門

JSON入門 レッスン1 JSON概要 - YouTube

JSONとはなんなのか?

  • ObjectとArrayの組み合わせ

Java Script Object Notation というくらいなので、

  • JavaScriptのオブジェクトをそのままの形で文字列化したもの
  • 構造としてkey・value形式
  • valueには配列もオブジェクトも入れられる

こんなの

animal.json
[ 
{ 
    "name": "Meowsy", 
    "species" : "cat", 
    "foods": { 
        "likes": ["tuna", "catnip"], 
        "dislikes": ["ham", "zucchini"] 
    } 
}, 
{ 
    "name": "Barky", 
    "species" : "dog", 
    "foods": { 
        "likes": ["bones", "carrots"], 
        "dislikes": ["tuna"] 
    } 
}, 
{ 
    "name": "Purrpaws", 
    "species" : "cat", 
    "foods": { 
        "likes": ["mice"], 
        "dislikes": ["cookies"] 
    } 
} 
] 

特徴

  • 平易なテキストデータ
  • XMLと違って軽い
  • 形式がJavaScriptのオブジェクトと同じ形式なので、プログラムで容易に取り込める

データ型

  • 文字列
  • 数値
  • ブーリア
  • 配列
    • [] で囲む
  • オブジェクト
    • {}で囲む
    • keyとvalueの形式にする必要がある

JSONLint - The JSON Validator

jsonの形式が正しいかどうかをvalidateしてくれるエディター

JavaScriptにて

オブジェクトをJSONに変換する

JSON.stringify(Animals)

JSONをオブジェクトに変換する

JSON.parse(Animals)
3
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?