使用 Java 或 Python 根据特定条件从复杂的 JSON 中提取值
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": Read Command Z meaning": Read
这意味着:对于每个A
,如果命令的含义是"Read"
,则提取命令然后提取通用命令"XYZ"
和命令编号。
还有 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
P.S。 : 注意 删除 的 meaning
键的 Read
值后的空白字符。
我对 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": Read Command Z meaning": Read
这意味着:对于每个A
,如果命令的含义是"Read"
,则提取命令然后提取通用命令"XYZ"
和命令编号。
还有 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
P.S。 : 注意 删除 的 meaning
键的 Read
值后的空白字符。