2016-06-28 155 views
0

的字典,我有以下JSON響應:解析字典

{'Securities': [{'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Computers & Peripherals', 'SecurityId': 2654, 'DisplayCode': 'AAPL', 'PricingSymbol': 'AAPL US Equity', 'PriceLast': 93.59, 'SecurityDesc': 'APPLE INC', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 92.04, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Information Technology'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Air Freight & Logistics', 'SecurityId': 2660, 'DisplayCode': 'AAWW', 'PricingSymbol': 'AAWW US Equity', 'PriceLast': 38.81, 'SecurityDesc': 'ATLAS AIR WORLDWIDE HOLDINGS', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 38.45, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Industrials'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Communications Equipment', 'SecurityId': 3445, 'DisplayCode': 'BBRY', 'PricingSymbol': 'BBRY US Equity', 'PriceLast': 6.48, 'SecurityDesc': 'BLACKBERRY LTD', 'Currency': 'USD', 'Country': 'CANADA', 'TradingFactor': 1.0, 'PriceStart': 6.29, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Information Technology'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': '', 'SecurityId': 403321, 'DisplayCode': 'ZGSI', 'PricingSymbol': 'ZGSI US Equity', 'PriceLast': 6.5, 'SecurityDesc': 'ZERO GRAVITY SOLUTIONS INC', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 0.0, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': ''}], 'Holdings': [{'HoldingId': 919049, 'MTDPnLTotal': -36434.0, 'Quantity': -40000.0, 'DTDPnLTotal': -36434.0, 'NetExposure': -3743600.0, 'LastModified': '2016-06-28T12:40:51.527', 'WTDPnLTotal': -36434.0, 'AcuId': 212, 'SecurityId': 2654, 'QuantityTraded': -40000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -36434.0, 'BetaExposure': -4155021.64, 'BookValue': -3743600.0, 'YTDPnLTotal': -36434.0, 'QuantityStart': 0.0, 'ShareEquivalent': -40000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1476408, 'MTDPnLTotal': -40.0, 'Quantity': -1000.0, 'DTDPnLTotal': -40.0, 'NetExposure': -38810.0, 'LastModified': '2016-06-28T12:38:03.72', 'WTDPnLTotal': -40.0, 'AcuId': 212, 'SecurityId': 2660, 'QuantityTraded': -1000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -40.0, 'BetaExposure': -63954.999, 'BookValue': -38810.0, 'YTDPnLTotal': -40.0, 'QuantityStart': 0.0, 'ShareEquivalent': -1000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1586376, 'MTDPnLTotal': -30200.0, 'Quantity': 10000.0, 'DTDPnLTotal': -30200.0, 'NetExposure': 64800.0, 'LastModified': '2016-06-28T13:14:42.237', 'WTDPnLTotal': -30200.0, 'AcuId': 212, 'SecurityId': 3445, 'QuantityTraded': 10000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -30200.0, 'BetaExposure': 92178.0, 'BookValue': 64800.0, 'YTDPnLTotal': -30200.0, 'QuantityStart': 0.0, 'ShareEquivalent': 10000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1586379, 'MTDPnLTotal': 0.0, 'Quantity': 100.0, 'DTDPnLTotal': 0.0, 'NetExposure': 650.0, 'LastModified': '2016-06-28T14:07:55.58', 'WTDPnLTotal': 0.0, 'AcuId': 212, 'SecurityId': 403321, 'QuantityTraded': 100.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': 0.0, 'BetaExposure': 650.0, 'BookValue': 650.0, 'YTDPnLTotal': 0.0, 'QuantityStart': 0.0, 'ShareEquivalent': 100.0, 'CustodianAccountCode': 'CSPB-739R70'}], 'LiveDate': '2016-06-28T00:00:00', 'RefreshTime': '2016-06-28T21:25:22.8529416Z'} 

這是詞典的詞典。我想檢索「AAPL」,這是「DisplayCode」鍵的值。

我嘗試以下,但它不工作:

for item in json_data: 
    print(item['Holdings']['DisplayCode']) 

回答

3

您需要索引列表從json_data["Securities"]回來,你想要的密鑰字典的索引爲0:

js = {'Securities': [{'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Computers & Peripherals', 'SecurityId': 2654, 'DisplayCode': 'AAPL', 'PricingSymbol': 'AAPL US Equity', 'PriceLast': 93.59, 'SecurityDesc': 'APPLE INC', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 92.04, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Information Technology'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Air Freight & Logistics', 'SecurityId': 2660, 'DisplayCode': 'AAWW', 'PricingSymbol': 'AAWW US Equity', 'PriceLast': 38.81, 'SecurityDesc': 'ATLAS AIR WORLDWIDE HOLDINGS', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 38.45, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Industrials'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': 'Communications Equipment', 'SecurityId': 3445, 'DisplayCode': 'BBRY', 'PricingSymbol': 'BBRY US Equity', 'PriceLast': 6.48, 'SecurityDesc': 'BLACKBERRY LTD', 'Currency': 'USD', 'Country': 'CANADA', 'TradingFactor': 1.0, 'PriceStart': 6.29, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': 'Information Technology'}, {'InvestmentType': 'Ordinary Share', 'SecurityUnderlierCode': '', 'GicsIndustry': '', 'SecurityId': 403321, 'DisplayCode': 'ZGSI', 'PricingSymbol': 'ZGSI US Equity', 'PriceLast': 6.5, 'SecurityDesc': 'ZERO GRAVITY SOLUTIONS INC', 'Currency': 'USD', 'Country': 'UNITED STATES', 'TradingFactor': 1.0, 'PriceStart': 0.0, 'AssetType': 'Equity', 'SecuritySubTypeCode': 'Equity', 'PricingFactor': 1.0, 'GicsSector': ''}], 'Holdings': [{'HoldingId': 919049, 'MTDPnLTotal': -36434.0, 'Quantity': -40000.0, 'DTDPnLTotal': -36434.0, 'NetExposure': -3743600.0, 'LastModified': '2016-06-28T12:40:51.527', 'WTDPnLTotal': -36434.0, 'AcuId': 212, 'SecurityId': 2654, 'QuantityTraded': -40000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -36434.0, 'BetaExposure': -4155021.64, 'BookValue': -3743600.0, 'YTDPnLTotal': -36434.0, 'QuantityStart': 0.0, 'ShareEquivalent': -40000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1476408, 'MTDPnLTotal': -40.0, 'Quantity': -1000.0, 'DTDPnLTotal': -40.0, 'NetExposure': -38810.0, 'LastModified': '2016-06-28T12:38:03.72', 'WTDPnLTotal': -40.0, 'AcuId': 212, 'SecurityId': 2660, 'QuantityTraded': -1000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -40.0, 'BetaExposure': -63954.999, 'BookValue': -38810.0, 'YTDPnLTotal': -40.0, 'QuantityStart': 0.0, 'ShareEquivalent': -1000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1586376, 'MTDPnLTotal': -30200.0, 'Quantity': 10000.0, 'DTDPnLTotal': -30200.0, 'NetExposure': 64800.0, 'LastModified': '2016-06-28T13:14:42.237', 'WTDPnLTotal': -30200.0, 'AcuId': 212, 'SecurityId': 3445, 'QuantityTraded': 10000.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': -30200.0, 'BetaExposure': 92178.0, 'BookValue': 64800.0, 'YTDPnLTotal': -30200.0, 'QuantityStart': 0.0, 'ShareEquivalent': 10000.0, 'CustodianAccountCode': 'CSPB-739R70'}, {'HoldingId': 1586379, 'MTDPnLTotal': 0.0, 'Quantity': 100.0, 'DTDPnLTotal': 0.0, 'NetExposure': 650.0, 'LastModified': '2016-06-28T14:07:55.58', 'WTDPnLTotal': 0.0, 'AcuId': 212, 'SecurityId': 403321, 'QuantityTraded': 100.0, 'StrategyCode': 'QIAN-GENERALIST', 'FundCode': 'AQTF', 'TradingPnL': 0.0, 'BetaExposure': 650.0, 'BookValue': 650.0, 'YTDPnLTotal': 0.0, 'QuantityStart': 0.0, 'ShareEquivalent': 100.0, 'CustodianAccountCode': 'CSPB-739R70'}], 'LiveDate': '2016-06-28T00:00:00', 'RefreshTime': '2016-06-28T21:25:22.8529416Z'} 

print(js["Securities"][0]["DisplayCode"]) 

那給你:

AAPL 

如果它可能出現在另一個索引和/或如果你想要一個LL的DisplayCodes:

for dct in js["Securities"]: 
    print(dct["DisplayCode"]) 

這將使你:

AAPL 
AAWW 
BBRY 
ZGSI 
+0

帕德里克·謝謝!這應該工作。 – user2829222

+0

不用擔心,我唯一看到的'DisplayCode'在Secutities項下的字典列表中,Holdings沒有這樣的信息。 –

+0

不用擔心!我得到了我想要的:)謝謝:) – user2829222