如何将 json 响应分解为特定于名称值的多个部分?

how to break down json response into multiple parts specific to the name value?

您好,我正在 api 调用 myfxbook api,在 return 中,我收到了一个包含过多数据的 json 响应,是否可以中断关于名称值是什么,对多个较小部分的大反应? 这是 Json 回复的大约 30%:

[{'name': 'EURUSD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 22685.66, 'longVolume': 47762.79, 'longPositions': 89968, 'shortPositions': 56612, 'totalPositions': 146580, 'avgShortPrice': 1.1678, 'avgLongPrice': 1.199}, {'name': 'GBPUSD', 'shortPercentage': 54, 'longPercentage': 46, 'shortVolume': 6462.95, 'longVolume': 5587.2, 'longPositions': 17783, 'shortPositions': 22240, 'totalPositions': 40023, 'avgShortPrice': 1.3592, 'avgLongPrice': 1.3929}, {'name': 'USDJPY', 'shortPercentage': 89, 'longPercentage': 11, 'shortVolume': 32447.66, 'longVolume': 4081.71, 'longPositions': 10941, 'shortPositions': 58722, 'totalPositions': 69663, 'avgShortPrice': 106.7526, 'avgLongPrice': 108.6437}, {'name': 'GBPJPY', 'shortPercentage': 79, 'longPercentage': 21, 'shortVolume': 2725.22, 'longVolume': 711.99, 'longPositions': 3210, 'shortPositions': 9743, 'totalPositions': 12953, 'avgShortPrice': 146.1479, 'avgLongPrice': 149.2243}, {'name': 'USDCAD', 'shortPercentage': 47, 'longPercentage': 53, 'shortVolume': 6235.58, 'longVolume': 6930.97, 'longPositions': 16121, 'shortPositions': 13276, 'totalPositions': 29397, 'avgShortPrice': 1.2658, 'avgLongPrice': 1.3025}, {'name': 'EURAUD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 562.13, 'longVolume': 1170.82, 'longPositions': 4444, 'shortPositions': 3004, 'totalPositions': 7448, 'avgShortPrice': 1.5434, 'avgLongPrice': 1.5816}, {'name': 'EURJPY', 'shortPercentage': 74, 'longPercentage': 26, 'shortVolume': 10500.41, 'longVolume': 3672.26, 'longPositions': 8166, 'shortPositions': 25254, 'totalPositions': 33420, 'avgShortPrice': 126.1881, 'avgLongPrice': 128.6925}, {'name': 'AUDCAD', 'shortPercentage': 48, 'longPercentage': 52, 'shortVolume': 1559.83, 'longVolume': 1699.36, 'longPositions': 5943, 'shortPositions': 6278, 'totalPositions': 12221, 'avgShortPrice': 0.9561, 'avgLongPrice': 0.979}, {'name': 'AUDJPY', 'shortPercentage': 83, 'longPercentage': 17, 'shortVolume': 1614.66, 'longVolume': 322.98, 'longPositions': 1905, 'shortPositions': 6048, 'totalPositions': 7953, 'avgShortPrice': 78.9751, 'avgLongPrice': 83.5306}, {'name': 'AUDNZD', 'shortPercentage': 63, 'longPercentage': 37, 'shortVolume': 673.48, 'longVolume': 403.95, 'longPositions': 2123, 'shortPositions': 3785, 'totalPositions': 5908, 'avgShortPrice': 1.0644, 'avgLongPrice': 1.0779}]

如您所见,'name':'EURUSD' 有多个值,如 shortpercentage 和 longpercerntage 、shortvolume 和 long volume 等

我正在尝试获取特定货币的这些 shortpercentage 和 longpercentage,而不是所有货币,

像这样:

EURUSD    32    68

GBPUSD    54    46

USDJPY    .......
:
:
:

我希望我的问题有道理,我已尽力解释

这是你想要的吗?

from tabulate import tabulate

data = [{'name': 'EURUSD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 22685.66, 'longVolume': 47762.79, 'longPositions': 89968, 'shortPositions': 56612, 'totalPositions': 146580, 'avgShortPrice': 1.1678, 'avgLongPrice': 1.199}, {'name': 'GBPUSD', 'shortPercentage': 54, 'longPercentage': 46, 'shortVolume': 6462.95, 'longVolume': 5587.2, 'longPositions': 17783, 'shortPositions': 22240, 'totalPositions': 40023, 'avgShortPrice': 1.3592, 'avgLongPrice': 1.3929}, {'name': 'USDJPY', 'shortPercentage': 89, 'longPercentage': 11, 'shortVolume': 32447.66, 'longVolume': 4081.71, 'longPositions': 10941, 'shortPositions': 58722, 'totalPositions': 69663, 'avgShortPrice': 106.7526, 'avgLongPrice': 108.6437}, {'name': 'GBPJPY', 'shortPercentage': 79, 'longPercentage': 21, 'shortVolume': 2725.22, 'longVolume': 711.99, 'longPositions': 3210, 'shortPositions': 9743, 'totalPositions': 12953, 'avgShortPrice': 146.1479, 'avgLongPrice': 149.2243}, {'name': 'USDCAD', 'shortPercentage': 47, 'longPercentage': 53, 'shortVolume': 6235.58, 'longVolume': 6930.97, 'longPositions': 16121, 'shortPositions': 13276, 'totalPositions': 29397, 'avgShortPrice': 1.2658, 'avgLongPrice': 1.3025}, {'name': 'EURAUD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 562.13, 'longVolume': 1170.82, 'longPositions': 4444, 'shortPositions': 3004, 'totalPositions': 7448, 'avgShortPrice': 1.5434, 'avgLongPrice': 1.5816}, {'name': 'EURJPY', 'shortPercentage': 74, 'longPercentage': 26, 'shortVolume': 10500.41, 'longVolume': 3672.26, 'longPositions': 8166, 'shortPositions': 25254, 'totalPositions': 33420, 'avgShortPrice': 126.1881, 'avgLongPrice': 128.6925}, {'name': 'AUDCAD', 'shortPercentage': 48, 'longPercentage': 52, 'shortVolume': 1559.83, 'longVolume': 1699.36, 'longPositions': 5943, 'shortPositions': 6278, 'totalPositions': 12221, 'avgShortPrice': 0.9561, 'avgLongPrice': 0.979}, {'name': 'AUDJPY', 'shortPercentage': 83, 'longPercentage': 17, 'shortVolume': 1614.66, 'longVolume': 322.98, 'longPositions': 1905, 'shortPositions': 6048, 'totalPositions': 7953, 'avgShortPrice': 78.9751, 'avgLongPrice': 83.5306}, {'name': 'AUDNZD', 'shortPercentage': 63, 'longPercentage': 37, 'shortVolume': 673.48, 'longVolume': 403.95, 'longPositions': 2123, 'shortPositions': 3785, 'totalPositions': 5908, 'avgShortPrice': 1.0644, 'avgLongPrice': 1.0779}]

parsed = [[d["name"], d["shortPercentage"], d["longPercentage"]] for d in data]
print(tabulate(parsed, headers=["Name", "Short %", "Long %"], tablefmt="sql"))

输出:

Name      Short %    Long %
------  ---------  --------
EURUSD         32        68
GBPUSD         54        46
USDJPY         89        11
GBPJPY         79        21
USDCAD         47        53
EURAUD         32        68
EURJPY         74        26
AUDCAD         48        52
AUDJPY         83        17
AUDNZD         63        37

根据我的假设,我希望这就是您要找的。

结果:

l=[{'name': 'EURUSD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 22685.66, 'longVolume': 47762.79, 'longPositions': 89968, 'shortPositions': 56612, 'totalPositions': 146580, 'avgShortPrice': 1.1678, 'avgLongPrice': 1.199}, {'name': 'GBPUSD', 'shortPercentage': 54, 'longPercentage': 46, 'shortVolume': 6462.95, 'longVolume': 5587.2, 'longPositions': 17783, 'shortPositions': 22240, 'totalPositions': 40023, 'avgShortPrice': 1.3592, 'avgLongPrice': 1.3929}, {'name': 'USDJPY', 'shortPercentage': 89, 'longPercentage': 11, 'shortVolume': 32447.66, 'longVolume': 4081.71, 'longPositions': 10941, 'shortPositions': 58722, 'totalPositions': 69663, 'avgShortPrice': 106.7526, 'avgLongPrice': 108.6437}, {'name': 'GBPJPY', 'shortPercentage': 79, 'longPercentage': 21, 'shortVolume': 2725.22, 'longVolume': 711.99, 'longPositions': 3210, 'shortPositions': 9743, 'totalPositions': 12953, 'avgShortPrice': 146.1479, 'avgLongPrice': 149.2243}, {'name': 'USDCAD', 'shortPercentage': 47, 'longPercentage': 53, 'shortVolume': 6235.58, 'longVolume': 6930.97, 'longPositions': 16121, 'shortPositions': 13276, 'totalPositions': 29397, 'avgShortPrice': 1.2658, 'avgLongPrice': 1.3025}, {'name': 'EURAUD', 'shortPercentage': 32, 'longPercentage': 68, 'shortVolume': 562.13, 'longVolume': 1170.82, 'longPositions': 4444, 'shortPositions': 3004, 'totalPositions': 7448, 'avgShortPrice': 1.5434, 'avgLongPrice': 1.5816}, {'name': 'EURJPY', 'shortPercentage': 74, 'longPercentage': 26, 'shortVolume': 10500.41, 'longVolume': 3672.26, 'longPositions': 8166, 'shortPositions': 25254, 'totalPositions': 33420, 'avgShortPrice': 126.1881, 'avgLongPrice': 128.6925}, {'name': 'AUDCAD', 'shortPercentage': 48, 'longPercentage': 52, 'shortVolume': 1559.83, 'longVolume': 1699.36, 'longPositions': 5943, 'shortPositions': 6278, 'totalPositions': 12221, 'avgShortPrice': 0.9561, 'avgLongPrice': 0.979}, {'name': 'AUDJPY', 'shortPercentage': 83, 'longPercentage': 17, 'shortVolume': 1614.66, 'longVolume': 322.98, 'longPositions': 1905, 'shortPositions': 6048, 'totalPositions': 7953, 'avgShortPrice': 78.9751, 'avgLongPrice': 83.5306}, {'name': 'AUDNZD', 'shortPercentage': 63, 'longPercentage': 37, 'shortVolume': 673.48, 'longVolume': 403.95, 'longPositions': 2123, 'shortPositions': 3785, 'totalPositions': 5908, 'avgShortPrice': 1.0644, 'avgLongPrice': 1.0779}]

for i in range(len(l)):
    print(l[i]["name"],end=" ")
    print(l[i]["shortPercentage"],end=" ")
    print(l[i]["longPercentage"],end=" ")
    print("\n")

输出:

EURUSD 32 68 

GBPUSD 54 46 

USDJPY 89 11 

GBPJPY 79 21 

USDCAD 47 53 

EURAUD 32 68 

EURJPY 74 26 

AUDCAD 48 52 

AUDJPY 83 17 

AUDNZD 63 37 

如果您只需要某些特定货币,请使用条件语句只打印需要的货币。

使用理解:

# data = this is an input big data
interested = ['EURUSD', 'GBPUSD']
result = [{'pair' : pair['name'],
           'short' : pair['shortPercentage'],
           'long' : pair['longPercentage']}
          for pair in data if pair['name'] in interested]
print(result)

输出是 shotred 列表:

[{'pair': 'EURUSD', 'short': 32, 'long': 68}, {'pair': 'GBPUSD', 'short': 54, 'long': 46}]