使用Java或Python根据特定条件从复杂的JSON中提取值
我对JSON一无所知。我有一个JSON文件,其中包含以下格式:使用Java或Python根据特定条件从复杂的JSON中提取值,java,python,json,conditional-statements,extract,Java,Python,Json,Conditional Statements,Extract,我对JSON一无所知。我有一个JSON文件,其中包含以下格式: {“A”: {“B”:[ {“C”:{“text”:“Command X”,“意为”:“Read”,“http”:“some link”,“Reference”:“Reference name”}, {“C”:{“text”:“Command Y”,“意为”:“Write”,“http”:“some link”}, {“C”:{“text”:“Command Z”,“意思是”:“Read”,“http”:“some link”} ]
{“A”:
{“B”:[
{“C”:{“text”:“Command X”,“意为”:“Read”,“http”:“some link”,“Reference”:“Reference name”},
{“C”:{“text”:“Command Y”,“意为”:“Write”,“http”:“some link”},
{“C”:{“text”:“Command Z”,“意思是”:“Read”,“http”:“some link”}
],
“上下文”:{“上下文id”:“6429”,“章节id”:“123”,“句子id”:“456”,“标题”:“某物”,“章节id”:“6”,“章节标题”:“某物”,“章节id”:“77”,“章节编号”:“1”,“句子编号”:“12”,“段落id”:“0000”,“小节标题”:“某物”},
“链接id”:“123”,“命令”:“XYZ”,“节链接”:“链接”,“命令编号”:“20.5.1”,“内容参考”:“某物”}
}
{“A”:
....
}
我需要摘录以下内容:
Command”:XYZ命令编号:20.5.1命令X含义:读取命令Z含义:读取
这意味着:对于每个
A
,如果命令的含义是“Read”
,则提取命令,然后提取常规命令“XYZ”“
和命令号。还有Java库FasterXml,它具有用于读取和写入类似Json的ObjectMapper的对象。就有条件地提取对象而言,有条件的部分取决于您。还有Java库FasterXml,其中包含用于读取和写入类似Json的ObjectMapper的对象。就条件提取对象而言,条件部分取决于您。您可以导入json
库并通过python
使用json.loads()
函数:
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
注意:注意在
读取含义
键的值后删除空格字符。您可以导入json
库并通过python
使用json.loads()
函数:
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
注意:在Read
value ofmeans
key之后删除空格字符时要小心。Python有专门的json模块来解析json文件。如果您喜欢python,请参阅python有专门的json模块来解析json文件。如果您喜欢python,请参考。非常感谢您的帮助,您的答案非常有效。欢迎@Zain感谢您的帮助,您的答案非常有效。欢迎@Zain