취미와 밥줄사이

[Python] JSON 자료형 다루기 본문

Python

[Python] JSON 자료형 다루기

취미와 밥줄사이 2021. 10. 26. 11:17

JSON


  • JSON는 데이터를 저장하고 교환하는 문법
  • JSON는 텍스트이며 자바스크립트 표기법을 이용함

Python에서 JSON 다루기


  • 파이썬 내장 패키지 json 호출
  • json 패키지는 JSON data를 다룰 때 사용
  • import json

JSON to Python


  • JSON string 파이썬 자료형으로 전환하기 위해서 json.loads() 메소드 사용
  • 파이썬의 딕셔너리 자료형으로 변환됨
  • # Convert from JSON to Python:
    
    import json
    
    # some Json
    x = '{"name":"Jhon", "age":30, "city":"New York"}'
    
    # parse x:
    y = json.loads(x)
    
    # the result is a python dictionary:
    print(y["age"] )

Python to JSON


  • 파이썬 객체를 JSON string로 전환하는 메소드는 json.dumps() 
  • # Convert from Python to JSON:
    import json
    
    # a Python object (dict):
    x = {
    	"name":"Jhon",
        "age":30,
        "city":"New York"
        }
      
    # convert into JSON:
    y = json.dumps(x)
    
    # the result is a JSON string:
    print(y)
  • JSON 자료형으로 변환가능한 파이썬 객체 리스트
    • dict
    • list
    • tuple
    • string
    • int
    • float
    • True
    • False
    • None
  • # Convert Python object into JSON strings, and print the values:
    import json
    
    print(json.dumps({"name":"John", "age":30}))
    print(json.dumps(["apple", "banndas"]))
    print(json.dumps(("apple", "bananas")))
    print(json.dumps("hello")
    print(json.dumps(42))
    print(json.dumps(31.76))
    print(json.dumps(True))
    print(json.dumps(False))
    print(json.dumps(None))
  • Python JSON
    dict Object
    list Array
    tuple Array
    str String
    int Number
    float Number
    True true
    False false
    None null

 

 

 

 

 

REFERENCE


https://www.w3schools.com/python/python_json.asp

 

Python JSON

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

'Python' 카테고리의 다른 글

[Python] Requests Module  (0) 2021.11.03
[Python] 문자열 Formatting  (0) 2021.10.26
[Python] PIP이란?  (0) 2021.10.26
[Anaconda ] - cmd창에서 사용하기  (0) 2021.03.30
[MySQL] - MySQL 라이브러리 (설치방법, 임포트 방법 등)  (0) 2021.03.18