我正试图解析Json并根据输入键拉值。JSON结构并不总是相同的以下是JSON结构示例,JSON以[]开始和结束,有时以{}
结束[
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"days": {
"weekdays": "Friday",
"weekends": "Sunday",
},
"batters": {
"batter": [
{
"id": "1001",
"type": "Regular"
},
{
"id": "1002",
"type": "Chocolate"
},
]
},
"topping": [
{
"id": "5001",
"type": "None"
},
{
"id": "5002",
"type": "Glazed"
},
]
}
]
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"days": {
"weekdays": "Friday",
"weekends": "Sunday",
},
"batters": {
"batter": [
{
"id": "1001",
"type": "Regular"
},
{
"id": "1002",
"type": "Chocolate"
},
]
},
"topping": [
{
"id": "5001",
"type": "None"
},
{
"id": "5002",
"type": "Glazed"
},
]
}
i tried below code
import org.json.simple.*;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public static void main(String[] args) throws IOException, Exception {
File InputFolder = new File(System.getProperty("user.dir") + "//Files//new.json");
JsonElementFromFile(InputFolder,"name");
}
public static void JsonElementFromFile(File FilePath, String key) throws IOException, Exception {
JSONParser parser = new JSONParser();
FileReader reader = new FileReader(FilePath);
JSONArray obj = (JSONArray) parser.parse(reader);
for (Object o : obj) {
JSONObject json = (JSONObject) o;
String jsonKeyValue = (String)json.get(key).toString();
System.out.println(key + " : " + jsonKeyValue); }
}
- 以上代码在JSON数据以"["但如果以"{"开头,则不起作用。,无论任何结构,我都应该能够得到value
- 我只能从第一个对象的关键字"id","类型","名称","但当我把钥匙传递给"天","工作日";它没有拉。即使我有更多的数组和对象,我应该能够通过传递键的路径来拉,例如:" batteries .batter[0].id"或"batters.batter [1] .type" * *
- 基本上我正在寻找可重用的代码拉值基于键传递,不想硬编码JSON中的任何值。
如果您不想硬编码不同的JSON结构,解决方案是使用JSON查询库。
https://github.com/octomix/josson
<反序列化数组/strong>
Josson array = Josson.fromJsonString(
"[" +
" {" +
" "id": "0001"," +
" "type": "donut"," +
" "name": "Cake"," +
" "ppu": 0.55," +
" "days": {" +
" "weekdays": "Friday"," +
" "weekends": "Sunday"" +
" }," +
" "batters": {" +
" "batter": [" +
" {" +
" "id": "1001"," +
" "type": "Regular"" +
" }," +
" {" +
" "id": "1002"," +
" "type": "Chocolate"" +
" }" +
" ]" +
" }," +
" "topping": [" +
" {" +
" "id": "5001"," +
" "type": "None"" +
" }," +
" {" +
" "id": "5002"," +
" "type": "Glazed"" +
" }" +
" ]" +
" }," +
" {" +
" "batters": {" +
" "batter": [" +
" {" +
" "id": "1003"," +
" "type": "Special"" +
" }," +
" {" +
" "id": "1004"," +
" "type": "DarkChocolate"" +
" }" +
" ]" +
" }" +
" }" +
"]");
<反序列化对象/strong>
Josson object = Josson.fromJsonString(
"{" +
" "id": "0001"," +
" "type": "donut"," +
" "name": "Cake"," +
" "ppu": 0.55," +
" "days": {" +
" "weekdays": "Friday"," +
" "weekends": "Sunday"" +
" }," +
" "batters": {" +
" "batter": [" +
" {" +
" "id": "1001"," +
" "type": "Regular"" +
" }," +
" {" +
" "id": "1002"," +
" "type": "Chocolate"" +
" }" +
" ]" +
" }," +
" "topping": [" +
" {" +
" "id": "5001"," +
" "type": "None"" +
" }," +
" {" +
" "id": "5002"," +
" "type": "Glazed"" +
" }" +
" ]" +
"}");
查询
System.out.println(array.getString("batters.batter[0].id")); // 1001
System.out.println(object.getString("batters.batter[0].id")); // 1001
System.out.println(array.getString("batters.batter[1].type")); // Chocolate
System.out.println(object.getString("batters.batter[1].type")); // Chocolate
System.out.println(array.getString("[0].batters.batter[1].id")); // 1002
System.out.println(array.getString("[1].batters.batter[1].type")); // DarkChocolate
System.out.println(array.getString("batters.batter[3].type")); // DarkChocolate
我找到了解决这个问题的简单方法。使用com.fasterxml.jackson,我们可以从JSON中提取任意值,
enter code here
filepath= place where file stored
nodepath= path of node
File File = new File(filePath);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonTree = objectMapper.readTree(File);
String value = jsonTree.at(nodepath).asText();
return value;