使用Java或Python根据特定条件从复杂的JSON中提取值。

huangapple go评论114阅读模式
英文:

Extract values from complicated JSON based on a specific condition using Java or Python

问题

我对JSON完全不了解。我有一个包含以下格式的JSON文件:

  1. {"A":
  2. {"B":[
  3. {"C":{"text":"Command X","meaning":"Read ","http":"some link","Reference":"Reference name"}},
  4. {"C":{"text":"Command Y","meaning":"Write","http":"some link"}},
  5. {"C":{"text":"Command Z","meaning":"Read","http":"some link"}}
  6. ],
  7. "context":{"context-id":"6429","section-id":"123","sentence-id":"456","title":"Something","chapter-id":"6","section-title":"Something","sentence-num-in-chapter":"30","section-id":"77","sentence-num-in-section":"1","num-of-sentences":"12","para-id":"0000","subsection-title":"something"},
  8. "link-id":"123","Command":"XYZ","Sectionlink":"link","command-number":"20.5.1","content-ref":"Something"}
  9. }
  10. {"A":
  11. ....
  12. }

我需要提取以下内容:

  1. Command":XYZ command-number: 20.5.1 Command X meaning" 读取 Command Z meaning": 读取

这意味着:对于每个“A”,如果命令的含义是“读取”,则提取通用命令“XYZ”和命令编号。

英文:

I am totally new to JSON. I have a JSON file that contains the following format:

  1. {"A":
  2. {"B":[
  3. {"C":{"text":"Command X","meaning":"Read ","http":"some link","Reference":"Reference name"}},
  4. {"C":{"text":"Command Y","meaning":"Write","http":"some link"}},
  5. {"C":{"text":"Command Z","meaning":"Read","http":"some link"}}
  6. ],
  7. "context":{"context-id":"6429","section-id":"123","sentence-id":"456","title":"Something","chapter-id":"6","section-title":"Something","sentence-num-in-chapter":"30","section-id":"77","sentence-num-in-section":"1","num-of-sentences":"12","para-id":"0000","subsection-title":"something"},
  8. "link-id":"123","Command":"XYZ","Sectionlink":"link","command-number":"20.5.1","content-ref":"Something"}
  9. }
  10. {"A":
  11. ....
  12. }

I need to extract the following:

  1. Command":XYZ command-number :20.5.1 Command X meaning": Read Command Z meaning": Read

Which means: For each A, extract the commands if the meaning of the command is "Read" then extract the general command "XYZ" and the command-number.

答案1

得分: 0

还有一个名为FasterXml的Java库,其中有用于读写Json的对象,比如ObjectMapper。在条件提取对象方面,条件部分取决于您。

英文:

There is also the Java library FasterXml that has objects for reading and writing Json - like ObjectMapper. In terms of conditionally extracting objects - the conditional part is on you.

答案2

得分: 0

  1. import json
  2. s = '{...}' # The JSON string you provided
  3. ds = json.loads(s)
  4. for dict_key in ds:
  5. if dict_key == 'A':
  6. A = ds[dict_key]
  7. for inner_dict in A:
  8. for B in A[inner_dict]:
  9. try:
  10. if B['C']['meaning'] == 'Read':
  11. print("text : ", B['C']['text'])
  12. print("Command : ", A['Command'])
  13. print("command-number : ", A['command-number'])
  14. except:
  15. exit
英文:

You can import json library and use json.loads() function through python :

  1. import json
  2. s = '{"A":{"B":[{"C":{"text":"Command X","meaning":"Read","http":"some link","Reference":"Reference name"}},{"C":{"text":"Command Y","meaning":"Write","http":"some link"}},{"C":{"text":"Command Z","meaning":"Read","http":"some link"}}],"context":{"context-id":"6429","section-id":"123","sentence-id":"456","title":"Something","chapter-id":"6","section-title":"Something","sentence-num-in-chapter":"30","section-id":"77","sentence-num-in-section":"1","num-of-sentences":"12","para-id":"0000","subsection-title":"something"},"link-id":"123","Command":"XYZ","Sectionlink":"link","command-number":"20.5.1","content-ref":"Something"}}'
  3. ds = json.loads(s)
  4. for dict in ds:
  5. if dict == 'A':
  6. A = ds[dict]
  7. for dict in A:
  8. for B in A[dict]:
  9. try:
  10. if B['C']['meaning']=='Read':
  11. print("text : ",B['C']['text'])
  12. print("Command : ",A['Command'])
  13. print("command-number : ",A['command-number'])
  14. except:
  15. exit

P.S. : Be careful about the removing of whitespace character after Read value of meaning key.

huangapple
  • 本文由 发表于 2020年9月14日 23:08:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63887050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定