json获取键/值对的数量

qv7cva1a  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(366)

关闭。这个问题需要更加关注。它目前不接受答案。
**想改进这个问题吗?**编辑这篇文章,更新这个问题,使它只关注一个问题。

两小时前关门了。
改进这个问题
我有一个来自api的json结果

[
   {
      "dstatus" : "A",
      "from" : "root@mail-relay.localdomain",
      "id" : "BB7241C1396",
      "msgid" : "<20210710080004.BB7241C1396@mail-relay.localdomain>",
      "qid" : "BB7241C1396",
      "relay" : "328AB1C1397",
      "size" : "1036",
      "time" : 1625904005,
      "to" : "root@mail-relay.localdomain"
   },
   {
      "client" : "1.2.3.4[1.2.3.4]",
      "dstatus" : "A",
      "from" : "account@example.com",
      "id" : "Q4F35F1C133DR76FD11C1397",
      "msgid" : "<6248493.209.1625904062514.JavaMail.Admin@Admin>",
      "qid" : "4F35F1C133D",
      "relay" : "76FD11C1397",
      "rstatus" : "2",
      "size" : "1019",
      "time" : 1625904063,
      "to" : "abc@xyz.com"
   },
   {
      "client" : "1.2.3.4[1.2.3.4]",
      "dstatus" : "A",
      "from" : "account@example.com",
      "id" : "Q443721C0CECR504971C1397",
      "msgid" : "<9180008.211.1625904438763.JavaMail.Admin@Admin>",
      "qid" : "443721C0CEC",
      "relay" : "504971C1397",
      "rstatus" : "2",
      "size" : "1019",
      "time" : 1625904439,
      "to" : "abc@xyz.com"
   },
   {
      "client" : "1.2.3.4[1.2.3.4]",
      "dstatus" : "A",
      "from" : "any@another.com",
      "id" : "QAA8CD1C133DRB63071C1397",
      "msgid" : "<23564177.213.1625905759183.JavaMail.Admin@Admin>",
      "qid" : "AA8CD1C133D",
      "relay" : "B63071C1397",
      "rstatus" : "2",
      "size" : "1020",
      "time" : 1625905759,
      "to" : "abc@xyz.com"
   },
   {
      "client" : "1.2.3.4[1.2.3.4]",
      "dstatus" : "A",
      "from" : "account@example.com",
      "id" : "Q00E151C133DR0B3451C1397",
      "msgid" : "<3813150.215.1625906092385.JavaMail.Admin@Admin>",
      "qid" : "00E151C133D",
      "relay" : "0B3451C1397",
      "rstatus" : "2",
      "size" : "1016",
      "time" : 1625906093,
      "to" : "abc@xyz.com"
   },
   {
      "client" : "1.2.3.4[1.2.3.4]",
      "dstatus" : "A",
      "from" : "account@example.com",
      "id" : "QA22DD1C0CECRACD3A1C1397",
      "msgid" : "<20283951.217.1625907395975.JavaMail.Admin@Admin>",
      "qid" : "A22DD1C0CEC",
      "relay" : "ACD3A1C1397",
      "rstatus" : "5",
      "size" : "1020",
      "time" : 1625907396,
      "to" : "abc@xyz.com"
   }
]

并且需要计算有多少“来自电子邮件”的“rstatus”=2
结果如下:

account@example.com 3
any@another.com 1

任何人都可以给我看一本在bash或python上做事情的指南
谢谢你的帮助

  • 看起来你的帖子大部分都是代码;请添加更多详细信息。

看起来你的帖子大部分都是代码;请添加更多详细信息。
只需添加一些要求和的行*

uplii1fm

uplii1fm1#

任何人都可以给我看一本在bash或python上做事情的指南
在bash上,向导是 man jq , man sortman uniq . 这些可以导致这个示例解决方案,它过滤 fromrstatus=="2" 并统计单个事件:

jq '.[]|select(.rstatus=="2").from' Result.JSON | sort | uniq -c
qrjkbowd

qrjkbowd2#

这可能对你有帮助,谢谢。

import json

一些json:

x =  """[
   {
      "dstatus" : "A",
      "from" : "root@mail-relay.localdomain",
      "id" : "BB7241C1396",
      "msgid" : "<20210710080004.BB7241C1396@mail-relay.localdomain>",
      "qid" : "BB7241C1396",
      "relay" : "328AB1C1397",
      "size" : "1036",
      "time" : 1625904005,
      "to" : "root@mail-relay.localdomain"
   },
   {
      "client" : "1.2.3.4[1.2.3.4]",
      "dstatus" : "A",
      "from" : "account@example.com",
      "id" : "Q4F35F1C133DR76FD11C1397",
      "msgid" : "<6248493.209.1625904062514.JavaMail.Admin@Admin>",
      "qid" : "4F35F1C133D",
      "relay" : "76FD11C1397",
      "rstatus" : "2",
      "size" : "1019",
      "time" : 1625904063,
      "to" : "abc@xyz.com"
   },
   {
      "client" : "1.2.3.4[1.2.3.4]",
      "dstatus" : "A",
      "from" : "account@example.com",
      "id" : "Q443721C0CECR504971C1397",
      "msgid" : "<9180008.211.1625904438763.JavaMail.Admin@Admin>",
      "qid" : "443721C0CEC",
      "relay" : "504971C1397",
      "rstatus" : "2",
      "size" : "1019",
      "time" : 1625904439,
      "to" : "abc@xyz.com"
   },
   {
      "client" : "1.2.3.4[1.2.3.4]",
      "dstatus" : "A",
      "from" : "any@another.com",
      "id" : "QAA8CD1C133DRB63071C1397",
      "msgid" : "<23564177.213.1625905759183.JavaMail.Admin@Admin>",
      "qid" : "AA8CD1C133D",
      "relay" : "B63071C1397",
      "rstatus" : "2",
      "size" : "1020",
      "time" : 1625905759,
      "to" : "abc@xyz.com"
   },
   {
      "client" : "1.2.3.4[1.2.3.4]",
      "dstatus" : "A",
      "from" : "account@example.com",
      "id" : "Q00E151C133DR0B3451C1397",
      "msgid" : "<3813150.215.1625906092385.JavaMail.Admin@Admin>",
      "qid" : "00E151C133D",
      "relay" : "0B3451C1397",
      "rstatus" : "2",
      "size" : "1016",
      "time" : 1625906093,
      "to" : "abc@xyz.com"
   },
   {
      "client" : "1.2.3.4[1.2.3.4]",
      "dstatus" : "A",
      "from" : "account@example.com",
      "id" : "QA22DD1C0CECRACD3A1C1397",
      "msgid" : "<20283951.217.1625907395975.JavaMail.Admin@Admin>",
      "qid" : "A22DD1C0CEC",
      "relay" : "ACD3A1C1397",
      "rstatus" : "5",
      "size" : "1020",
      "time" : 1625907396,
      "to" : "abc@xyz.com"
   }
]
"""

解析x:

j_data = json.loads(x)
set_status_with_tow = {}

for data in j_data:
   if data.get('rstatus') == "2":
      if data.get('from') not in set_status_with_tow:
         set_status_with_tow[data.get('from')] = 1
      else:
         old_cunt = set_status_with_tow[data.get('from')]
         old_cunt = old_cunt + 1
         set_status_with_tow[data.get('from')] = old_cunt
   else:
      pass

for r_status_two in set_status_with_tow:
   print r_status_two, set_status_with_tow[r_status_two]

相关问题