gpt4 book ai didi

java - 将 JSON 字符串反序列化为字符串数组

转载 作者:行者123 更新时间:2023-12-02 00:06:58 26 4
gpt4 key购买 nike

如何获取这个字符串并放入String Array或HashMap中?有引用吗?

     {
"code" : 403,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "The domain policy has disabled third-party Drive apps",
"reason" : "domainPolicy"
} ],
"message" : "The domain policy has disabled third-party Drive apps"
}

====

这是我为 jackson 提供的解决方案:

public class TheErrorModel {
public int code;
public Collection<Error> errors;
public String message;

public TheErrorModel() {
}

public TheErrorModel(String json)
throws Exception {
ObjectMapper mapper = new ObjectMapper();
TheErrorModelother = mapper.readValue(json, TheErrorModel.class);

this.code = other.code;
this.errors = other.errors;
this.message = other.message;
}
}

class Error {
public String domain;
public String location;
public String locationType;
public String message;
public String reason;
}

TheErrorModel theErrorModel = new TheErrorModel(json);

所以我得到了类似 theErrorModel.message 的东西:D

最佳答案

您可以尝试使用GSON http://code.google.com/p/google-gson/ ,使用起来非常简单。您应该为此 json 模式创建类结构。

import com.google.gson.Gson;
class YourErrorsJSON{
private String domain
private String location;
private String locationType;
private String message;
private String reason
}

class YourJSON {
private int code;
private YourErrorsJSON[] errors;
private String message;
}
Gson gson = new Gson();
YourJSON obj = gson.fromJson(json, YourJSON.class);

关于java - 将 JSON 字符串反序列化为字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13640518/

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