使用 GSON 将 JSON 解析为 Java 对象
Parse JSON to Java Object using GSON
我正在尝试将 json 文档从 jenkins API 映射到我自己的 java 对象。 json 文档如下所示:
{
"assignedLabels": [
{}
],
"mode": "EXCLUSIVE",
"nodeDescription": "Jenkins Master-Knoten",
"nodeName": "",
"numExecutors": 2,
"description": null,
"jobs": [
{
"name": "Job 1",
"url": "https://build.example.com/jenkins/job/Job1/",
"color": "disabled"
},
{
"name": "Job 2",
"url": "https://build.example.com/jenkins/job/Job2/",
"color": "blue"
}
],
"overallLoad": {},
"primaryView": {
"name": "Alle",
"url": "https://build.example.com/jenkins/"
},
"quietingDown": false,
"slaveAgentPort": 0,
"unlabeledLoad": {},
"useCrumbs": false,
"useSecurity": true,
"views": [
{
"name": "Selection",
"url": "https://build.example.com/jenkins/view/-All%Selection/"
},
{
"name": "All",
"url": "https://build.example.com/jenkins/"
}
]
}
我的 java 模型如下所示:
public class JenkinsServer {
private List<String> assignedLabels;
private String url;
private String mode;
private String nodeName;
private String nodeDescription;
private String description;
private boolean useSecurity;
private boolean quietingDown;
private JenkinsServerView primaryView;
private List<JenkinsServerView> views;
private List<JenkinsJob> jobs;
// getters & setters
}
我现在做的就是打电话给
Gson gson = new Gson();
JenkinsServer server = gson.fromJson( reader, JenkinsServer.class );
但是我收到了这个异常
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 6 column 5
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
我一直在互联网上搜索解决方案,但无法弄清楚我做错了什么。也许你们知道:-)
正如@Gimby 在评论中提到的,问题出在您的 json 数据中。
解决方案1:
因为 assignedLabels
是一个空值的字符串数组,一个选项是改变 json 没有大括号的数据,如下所示:
"assignedLabels": []
解决方案2:
将transient
关键字添加到java class
中的变量
private transient List<String> assignedLabels;
解决方案3:
最后一个笨拙的实现方法是在 java class
中为要解析的所有变量添加 @Expose
注释
public class JenkinsServer {
private List<String> assignedLabels;
@Expose
private String url;
@Expose
private String mode;
@Expose
private String nodeName;
@Expose
private String nodeDescription;
@Expose
private String description;
@Expose
private boolean useSecurity;
@Expose
private boolean quietingDown;
@Expose
private JenkinsServerView primaryView;
@Expose
private List<JenkinsServerView> views;
@Expose
private List<JenkinsJob> jobs;
// getters & setters methods
}
然后添加解析逻辑以将您的 json 字符串解析为相应的 java 对象,如下所示:
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
JenkinsServer server = gson.fromJson(reader, JenkinsServer.class);
我正在尝试将 json 文档从 jenkins API 映射到我自己的 java 对象。 json 文档如下所示:
{
"assignedLabels": [
{}
],
"mode": "EXCLUSIVE",
"nodeDescription": "Jenkins Master-Knoten",
"nodeName": "",
"numExecutors": 2,
"description": null,
"jobs": [
{
"name": "Job 1",
"url": "https://build.example.com/jenkins/job/Job1/",
"color": "disabled"
},
{
"name": "Job 2",
"url": "https://build.example.com/jenkins/job/Job2/",
"color": "blue"
}
],
"overallLoad": {},
"primaryView": {
"name": "Alle",
"url": "https://build.example.com/jenkins/"
},
"quietingDown": false,
"slaveAgentPort": 0,
"unlabeledLoad": {},
"useCrumbs": false,
"useSecurity": true,
"views": [
{
"name": "Selection",
"url": "https://build.example.com/jenkins/view/-All%Selection/"
},
{
"name": "All",
"url": "https://build.example.com/jenkins/"
}
]
}
我的 java 模型如下所示:
public class JenkinsServer {
private List<String> assignedLabels;
private String url;
private String mode;
private String nodeName;
private String nodeDescription;
private String description;
private boolean useSecurity;
private boolean quietingDown;
private JenkinsServerView primaryView;
private List<JenkinsServerView> views;
private List<JenkinsJob> jobs;
// getters & setters
}
我现在做的就是打电话给
Gson gson = new Gson();
JenkinsServer server = gson.fromJson( reader, JenkinsServer.class );
但是我收到了这个异常
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 6 column 5
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
我一直在互联网上搜索解决方案,但无法弄清楚我做错了什么。也许你们知道:-)
正如@Gimby 在评论中提到的,问题出在您的 json 数据中。
解决方案1:
因为 assignedLabels
是一个空值的字符串数组,一个选项是改变 json 没有大括号的数据,如下所示:
"assignedLabels": []
解决方案2:
将transient
关键字添加到java class
private transient List<String> assignedLabels;
解决方案3:
最后一个笨拙的实现方法是在 java class
中为要解析的所有变量添加@Expose
注释
public class JenkinsServer {
private List<String> assignedLabels;
@Expose
private String url;
@Expose
private String mode;
@Expose
private String nodeName;
@Expose
private String nodeDescription;
@Expose
private String description;
@Expose
private boolean useSecurity;
@Expose
private boolean quietingDown;
@Expose
private JenkinsServerView primaryView;
@Expose
private List<JenkinsServerView> views;
@Expose
private List<JenkinsJob> jobs;
// getters & setters methods
}
然后添加解析逻辑以将您的 json 字符串解析为相应的 java 对象,如下所示:
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
JenkinsServer server = gson.fromJson(reader, JenkinsServer.class);