안녕하세요

프로그램 과정에서 막혔던 문제들에 대한 해결책 정리


페이지 목록

2013년 2월 13일 수요일

Json Example (Json 포멧)

JSON (JavaScript Object Notation)

JSON is an extremely light text-based open standard designed for human-readable data. It originates from the JavaScript language and is used representing simple data types, arrays and associative arrays (called objects).

JSON Example

ArrayArray elements are generally of a basic type (number, string, boolean, or null), however can also be a nested array or object (see Data Structures). Elements are comma-delimited and contained within brackets.
myArray = [ "John Doe", 29, true, null ]
myArray = [ [], {} ]
myArray[2] returns true

Array with objectsThis array contains comma-delimited objects which each contain multiple comma-delimited key:value pairs. Objects within an array can be accessed using the array name and index.
myArray = [
 { "name": "John Doe", "age": 29 }, 
 { "name": "Anna Smith", "age": 24 }, 
 { "name": "Peter Jones", "age": 39 }
]
myArray[0].name returns John Doe

ObjectThis object contains multiple comma-delimited key:value pairs. Object properties can be accessed using the the object name followed by a period and property name -or- can be accessed like an array using the property name in quotes (in place of an index).
myObject = {
 "first": "John",
 "last": "Doe",
 "age": 39,
 "sex": "M",
 "salary": 70000,
 "registered": true
}
myObject.salary returns 70000
myObject["salary"] returns 70000

Object with nested arrayThis object contains multiple comma-delimited key:value pairs and a nested array. The nested array can be accessed with the object name, property or 'key' containing the array and an array index.
myObject = {
 "first": "John",
 "last": "Doe",
 "age": 39,
 "sex": "M",
 "salary": 70000,
 "registered": true,
 "interests": [ "Reading", "Mountain Biking", "Hacking" ]
}
myObject.interests[0] returns Reading
myObject["interests"][0] returns Reading

Object with nested objectThis object contains multiple comma-delimited key:value pairs and a nested object. To access an object within an object, property names or 'keys' can be chained together -or- property names can be used like an array in place of indexes.
myObject = {
 "first": "John",
 "last": "Doe",
 "age": 39,
 "sex": "M",
 "salary": 70000,
 "registered": true,
 "favorites": {
  "color": "Blue",
  "sport": "Soccer",
  "food": "Spaghetti"
 } 
}
myObject.favorites.food returns Spaghetti
myObject["favorites"]["food"] returns Spaghetti

Object with nested arrays and objectsThis object is more complex and typical of what might come from an API. It contains key:value pairs, nested arrays and nested objects. Any of its elements can be accessed with a combination of the above techniques.
myObject = {
 "first": "John",
 "last": "Doe",
 "age": 39,
 "sex": "M",
 "salary": 70000,
 "registered": true,
 "interests": [ "Reading", "Mountain Biking", "Hacking" ],
 "favorites": {
  "color": "Blue",
  "sport": "Soccer",
  "food": "Spaghetti"
 }, 
 "skills": [
  {
   "category": "Javascript",
   "tests": [
    { "name": "One", "score": 90 },
    { "name": "Two", "score": 96 }
   ] 
  },
  {
   "category": "CouchDB",
   "tests": [
    { "name": "One", "score": 79 },
    { "name": "Two", "score": 84 }
   ] 
  },
  {
   "category": "Node.js",
   "tests": [
    { "name": "One", "score": 97 },
    { "name": "Two", "score": 93 }
   ] 
  }
 ]
}
myObject.skills[0].category returns Javascript
myObject["skills"][0]["category"] returns Javascript
myObject.skills[1].tests[0].score returns 79
myObject["skills"][1]["tests"][0]["score"] returns 79

Data Structures

Numbervar myNum = 123.456Double precision floating-point format in JavaScript; generally depends on implementation.
Stringvar myString = "abcdef"Series of characters (letters, numbers, or symbols); double-quoted UTF-8 with backslash escaping.
Booleanvar myBool = true(true or false)
Arrayvar myArray = [ "a", "b", "c", "d" ]Sequence of values, comma-separated and enclosed in square brackets; values don't need to be of the same type.
Objectvar myObject = { "id": 007 }Unordered collection of key:value pairs; comma-separated and enclosed in curly braces; the should be strings and be distinct.
Nullvar myNull = null(empty)

JSON Resources

JSONLintJSON validator


출처 :http://www.jsonexample.com/

Array

myArray = [ "John Doe", 29, true, null ]
myArray[2] returns true 

Array 형식은 myArray[0] ,myArray[1] 처럼 평소 사용하던 Array 사용 방법으로 사용할 수 있다.
0은 John Doe, 1 은 29를 반환한다.

Array with objects

myArray = [
 { "name": "John Doe", "age": 29 }, 
 { "name": "Anna Smith", "age": 24 }, 
 { "name": "Peter Jones", "age": 39 }
]
Array에 Object를 넣는 방법이다.

myArray[0].name returns John Doe
myArray[1].age 는 24를 return 하게 된다.

Object

object는 ":" 값을 기준으로 Key 값과 실제 값으로 매칭된다.
myObject = { "first": "John", "last": "Doe", "age": 39, "sex": "M", "salary": 70000, "registered": true }
myObject.salary returns 70000 myObject["salary"] returns 70000
위 두가지 방법으로 각각의 object를 추출 할 수 있다.
myobject.first 는 John을 반환하고,
myObject["first"] 로도 John을 반환할 수 있다.

Object with nested array

iterests 를 보면, interest 키 값에 Array가 대응될 수 있음을 알 수 있다.
myObject = {
 "first": "John",
 "last": "Doe",
 "age": 39,
 "sex": "M",
 "salary": 70000,
 "registered": true,
 "interests": [ "Reading", "Mountain Biking", "Hacking" ]
}
myObject.interests[0] returns Reading
myObject["interests"][0] returns Reading 
 
Object with nested arrays and objects

myObject = {
 "first": "John",
 "last": "Doe",
 "age": 39,
 "sex": "M",
 "salary": 70000,
 "registered": true,
 "interests": [ "Reading", "Mountain Biking", "Hacking" ],
 "favorites": {
  "color": "Blue",
  "sport": "Soccer",
  "food": "Spaghetti"
 }, 
 "skills": [
  {
   "category": "Javascript",
   "tests": [
    { "name": "One", "score": 90 },
    { "name": "Two", "score": 96 }
   ] 
  },
  {
   "category": "CouchDB",
   "tests": [
    { "name": "One", "score": 79 },
    { "name": "Two", "score": 84 }
   ] 
  },
  {
   "category": "Node.js",
   "tests": [
    { "name": "One", "score": 97 },
    { "name": "Two", "score": 93 }
   ] 
  }
 ]
}
myObject.skills[0].category returns Javascript
myObject["skills"][0]["category"] returns Javascript
myObject.skills[1].tests[0].score returns 79
myObject["skills"][1]["tests"][0]["score"] returns 79

위와 같이 복잡한 관계도 만들어 낼 수 있다.

기본 개념은 Array와 Object를 이용하여, Data를 Object Oriented 하게 만들어 내는 것인 듯 하다.