gpt4 book ai didi

java - 遍历动态JSON来读取每个子节点的属性

转载 作者:行者123 更新时间:2023-12-01 16:30:41 24 4
gpt4 key购买 nike

我有一些 JSON 可能会发生变化,但有一个常数是它将始终包含具有两个属性的多个对象; 文本answerText。一个 JSON 示例是

{
"food": {
"eggTolerance": {
"answerText": "None",
"text": "What is your egg tolerance?"
},
"lactoseTolerance": null
},
"cookingExperience": {
"experienceInLastFiveYears": {
"answerText": "Yes",
"text": "Was this experience within the last 5 years?"
},
"numberOfPies": {
"answerText": "More than 50",
"text": "How many pies have you baked?"
},
"significantPies": {
"answerText": "More than 50",
"text": "How many of these pies per quarter were at tasty?"
},
"spanOfExperience": {
"answerText": "Yes",
"text": "Do you have at least 12 months' experience baking pies?"
}
},
"cocktails": {
"manhattans": {
"answerText": "The kiss of death",
"text": "What have I done to deserve this flat, flavourless Manhattan?"
},
"Gin Martini": null
},
"waitressing": null
}

这可以通过使其更深或更宽来改变。例如,lactoseTolerance 可以添加另一个对象,或者可以将另一个对象添加到根对象。

如何遍历这个对象来访问每一个对象,从而获取最深层对象的属性?

我见过this示例但这只是给了我第一个级别。在这种情况下,我知道我可以迭代这些对象的子对象,但如果层次结构发生变化,实现就会被破坏。

最佳答案

我使用了 GSON 库:请尝试以下代码:

pom.xml

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>

然后我创建了 QA 类,它具有固定的两个属性;文本和答案文本

import com.google.gson.annotations.SerializedName;

public class QA {
@SerializedName("answerText")
private String answerText;
@SerializedName("text")
private String text;

public QA(String answerText, String text) {
this.answerText = answerText;
this.text = text;
}

@Override
public String toString() {
return "QA{" +
"answerText='" + answerText + '\'' +
", text='" + text + '\'' +
'}';
}


public String getAnswerText() {
return answerText;
}

public void setAnswerText(String answerText) {
this.answerText = answerText;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}


}

现在在驱动程序代码中:

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.Map;

public class Main {
public static void main(String[] args) {
System.out.println("running!!");
InputStream inputStream = Main.class.getResourceAsStream("json.json");
String json = "{\n" +
" \"food\": {\n" +
" \"eggTolerance\": {\n" +
" \"answerText\": \"None\",\n" +
" \"text\": \"What is your egg tolerance?\"\n" +
" },\n" +
" \"lactoseTolerance\": null\n" +
" },\n" +
" \"cookingExperience\": {\n" +
" \"experienceInLastFiveYears\": {\n" +
" \"answerText\": \"Yes\",\n" +
" \"text\": \"Was this experience within the last 5 years?\"\n" +
" },\n" +
" \"numberOfPies\": {\n" +
" \"answerText\": \"More than 50\",\n" +
" \"text\": \"How many pies have you baked?\"\n" +
" },\n" +
" \"significantPies\": {\n" +
" \"answerText\": \"More than 50\",\n" +
" \"text\": \"How many of these pies per quarter were at tasty?\"\n" +
" },\n" +
" \"spanOfExperience\": {\n" +
" \"answerText\": \"Yes\",\n" +
" \"text\": \"Do you have at least 12 months' experience baking pies?\"\n" +
" }\n" +
" },\n" +
" \"cocktails\": {\n" +
" \"manhattans\": {\n" +
" \"answerText\": \"The kiss of death\",\n" +
" \"text\": \"What have I done to deserve this flat, flavourless Manhattan?\"\n" +
" },\n" +
" \"Gin Martini\": null\n" +
" },\n" +
" \"waitressing\": null\n" +
"}";

final Gson gson = new Gson();


Type type = new TypeToken<Map<String, Map<String, QA>>>(){}.getType();
Map<String, Map<String, QA>> myMap = gson.fromJson(json, type);
System.out.println("Data:"+ myMap.get("food").get("eggTolerance").getAnswerText());


}


}

关于java - 遍历动态JSON来读取每个子节点的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62047846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com