使用GSON将JSON解析为Java对象

时间:2015-01-07 13:04:43

标签: java json parsing jenkins gson

我正在尝试将jenkins API中的json文档映射到我自己的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)

我一直在网上寻找解决方案,但无法弄清楚我做错了什么。也许你们知道: - )

1 个答案:

答案 0 :(得分:1)

正如@Gimby在评论中提到的,问题出在你的json数据中。

<强>解决方法1:

由于assignedLabels是一个具有空值的字符串数组,因此一个选项是更改没有花括号的json数据,如下所示:

"assignedLabels": []

<强>溶液2:

transient关键字添加到java类

中的变量中
private transient List<String> assignedLabels;

<强> Solution3:

最后一个笨拙的方法是将@Expose注释添加到要在java类中解析的所有变量

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);