2017-12-27 1 views
-2

나는이 json을 가지고 있으며 누군가가 나를 '1'을 얻는 것으로 도울 수 있는지 궁금해하고 있습니다. 다음 json에 대해 'open'값을 사용합니다. 파이썬 사용하기. 감사합니다Python에서 Json을 트래버스하기

{ 
"Meta Data": { 
    "1. Information": "Daily Prices (open, high, low, close) and Volumes", 
    "2. Symbol": "AAPL", 
    "3. Last Refreshed": "2017-12-26", 
    "4. Output Size": "Compact", 
    "5. Time Zone": "US/Eastern" 
}, 
"Time Series (Daily)": { 
    "2017-12-26": { 
     "1. open": "170.8000", 
     "2. high": "171.4700", 
     "3. low": "169.6790", 
     "4. close": "170.5700", 
     "5. volume": "33106577" 
    }, 
    "2017-12-22": { 
     "1. open": "174.6800", 
     "2. high": "175.4240", 
     "3. low": "174.5000", 
     "4. close": "175.0100", 
     "5. volume": "16052615" 
    }, 
    "2017-12-21": { 
     "1. open": "174.1700", 
     "2. high": "176.0200", 
     "3. low": "174.1000", 
     "4. close": "175.0100", 
     "5. volume": "20356826" 
    }, 
+1

사용하는 모듈은'파이썬의 사전 – furas

+0

로 변환 json' 당신이 ['json'] (https://docs.python.org/3/library/json.html) 모듈을 읽어 있나요? 그것은 꽤 자명하다. json 텍스트를'list' 및'dict' 구조체로 변환하기 만하면 일반 중첩 사전으로 데이터를 검색 할 수 있습니다. – SCB

+0

[Parse JSON in Python]의 가능한 복제본 (https://stackoverflow.com/questions/7771011/parse-json-in-python) – Galen

답변

0

사용 json 모듈은 파이썬의 사전에 JSON 변환합니다. 그리고 그것은 쉽습니다.

data = '''{ 
    "Meta Data": { 
     "1. Information": "Daily Prices (open, high, low, close) and Volumes", 
     "2. Symbol": "AAPL", 
     "3. Last Refreshed": "2017-12-26", 
     "4. Output Size": "Compact", 
     "5. Time Zone": "US/Eastern" 
    }, 
    "Time Series (Daily)": { 
     "2017-12-26": { 
      "1. open": "170.8000", 
      "2. high": "171.4700", 
      "3. low": "169.6790", 
      "4. close": "170.5700", 
      "5. volume": "33106577" 
     }, 
     "2017-12-22": { 
      "1. open": "174.6800", 
      "2. high": "175.4240", 
      "3. low": "174.5000", 
      "4. close": "175.0100", 
      "5. volume": "16052615" 
     }, 
     "2017-12-21": { 
      "1. open": "174.1700", 
      "2. high": "176.0200", 
      "3. low": "174.1000", 
      "4. close": "175.0100", 
      "5. volume": "20356826" 
     } 
    } 
}''' 

import json 

data = json.loads(data) 

for key, val in data['Time Series (Daily)'].items(): 
    print(key, val["1. open"]) 

결과 : 당신이 구조에 문제가있는 경우

2017-12-26 170.8000 
2017-12-22 174.6800 
2017-12-21 174.1700 

당신은 alwasy이 사전이 경우 키를 참조하는 항목의 유형 및 print(item.keys())을 표시 print(item)print(type(item))를 사용할 수 있습니다.

print(type(data)) 
print(data.keys()) 

# <class 'dict'> 
# dict_keys(['Meta Data', 'Time Series (Daily)']) 

print(type(data['Time Series (Daily)'])) 
print(data['Time Series (Daily)'].keys()) 

# <class 'dict'> 
# dict_keys(['2017-12-26', '2017-12-22', '2017-12-21']) 
+0

고마워! 이것은 효과가있다. –

관련 문제