英文:
Extract values from complicated JSON based on a specific condition using Java or Python
问题
我对JSON完全不了解。我有一个包含以下格式的JSON文件:
{"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"}
}
{"A":
....
}
我需要提取以下内容:
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:
{"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"}
}
{"A":
....
}
I need to extract the following:
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
import json
s = '{...}' # The JSON string you provided
ds = json.loads(s)
for dict_key in ds:
if dict_key == 'A':
A = ds[dict_key]
for inner_dict in A:
for B in A[inner_dict]:
try:
if B['C']['meaning'] == 'Read':
print("text : ", B['C']['text'])
print("Command : ", A['Command'])
print("command-number : ", A['command-number'])
except:
exit
英文:
You can import json
library and use json.loads()
function through python
:
import json
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"}}'
ds = json.loads(s)
for dict in ds:
if dict == 'A':
A = ds[dict]
for dict in A:
for B in A[dict]:
try:
if B['C']['meaning']=='Read':
print("text : ",B['C']['text'])
print("Command : ",A['Command'])
print("command-number : ",A['command-number'])
except:
exit
P.S. : Be careful about the removing of whitespace character after Read
value of meaning
key.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论