Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 提取重复的JSON标记_Python_Json - Fatal编程技术网

Python 提取重复的JSON标记

Python 提取重复的JSON标记,python,json,Python,Json,我正在寻找一种方法来提取相关的“市场”和“指标标签”。我使用了此代码,但它只执行一个提取步骤: nestedData = """{"dashboardId":8,"evalMatches":[{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: BBP 1H

我正在寻找一种方法来提取相关的“市场”和“指标标签”。我使用了此代码,但它只执行一个提取步骤:

nestedData = """{"dashboardId":8,"evalMatches":[{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: BBP 1H market: ORN/USDT }","tags":{"creation_date":"2020-11-26 10:29:13","indicator_label":"BBP 1H","market":"ORN/USDT"}},{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: STOCHRSI 1H market: UTK/BTC }","tags":{"creation_date":"2020-11-26 10:29:13","indicator_label":"STOCHRSI 1H","market":"UTK/BTC"}},{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: STOCHRSI 4H market: ORN/USDT }","tags":{"creation_date":"2020-11-26 10:29:13","indicator_label":"STOCHRSI 4H","market":"ORN/USDT"}}],"orgId":1,"panelId":14,"ruleId":52,"ruleName":"test alert","ruleUrl":"http://localhost:3000/d/1xBsMrIMk/alert?tab=alert\u0026editPanel=14\u0026orgId=1","state":"alerting","tags":{},"title":"[Alerting] test alert"}"""
我需要这样的结果

json_obj = json.loads(cts)
market = (json_obj["evalMatches"][0]["tags"]["market"])
indicator_label = (json_obj["evalMatches"][1]["tags"]["indicator_label"])
creation_date = (json_obj["evalMatches"][0]["tags"]["creation_date"])
您可以尝试:

market='ORN/USDT',indicator_label'BBP 1H,STOCHRSI 4H'
market='UTK/USDT',indicator_label'STOCHRSI 1H'
输出:

import json
nestedData = """{"dashboardId":8,"evalMatches":[{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: BBP 1H market: ORN/USDT }","tags":{"creation_date":"2020-11-26 10:29:13","indicator_label":"BBP 1H","market":"ORN/USDT"}},{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: STOCHRSI 1H market: UTK/BTC }","tags":{"creation_date":"2020-11-26 10:29:13","indicator_label":"STOCHRSI 1H","market":"UTK/BTC"}},{"value":1,"metric":"newdb.count { creation_date: 2020-11-26 10:29:13 indicator_label: STOCHRSI 4H market: ORN/USDT }","tags":{"creation_date":"2020-11-26 10:29:13","indicator_label":"STOCHRSI 4H","market":"ORN/USDT"}}],"orgId":1,"panelId":14,"ruleId":52,"ruleName":"test alert","ruleUrl":"http://localhost:3000/d/1xBsMrIMk/alert?tab=alert\u0026editPanel=14\u0026orgId=1","state":"alerting","tags":{},"title":"[Alerting] test alert"}"""
data = json.loads(nestedData)

output = {}
for val in data["evalMatches"]:
    if val["tags"]["market"] not in output:
        output[val["tags"]["market"]] = []
    output[val["tags"]["market"]].append(str(val["tags"]["indicator_label"]))

for data in output:
    print("market='" + data + "',indicator_label'" + ",".join(output[data]) + "'" )

不是要求的输出@navid@AbhigyanJaiswal:修改答案
market='ORN/USDT',indicator_label'BBP 1H,STOCHRSI 4H'
market='UTK/BTC',indicator_label'STOCHRSI 1H'