如何使用 Gson 为这个 Json 文件设置响应 class?
How do you set up a response class using Gson for this Json file?
我已经尝试 http://www.jsonschema2pojo.org/ 来解决这个问题,但没有成功。我正在尝试为此 Json 格式创建 Json 响应 class:
{
"Structure1": [
[
"StringValue1",
"StringValue2"
],
[
"StringValue1",
"StringValue2"
]
],
"Structure2": [
[
"StringValue1",
"StringValue2"
]
],
"Structure3": [
[
"StringValue1",
"StringValue2"
]
]
}
这是我当前的 class 的样子:
public class Response {
private HashMap<String, ArrayList<ArrayList<String>>> map = new HashMap<String, ArrayList<ArrayList<String>>>();
public HashMap<String, ArrayList<ArrayList<String>>> getMap() {
return map;
}
public void setFile11Txt(HashMap<String, ArrayList<ArrayList<String>>> map) {
this.map = map;
}
}
解析我在做
Response response = gson.fromJson(response, Response.class);
返回的地图最后是空的,我做错了什么?
TypeToken objtype= new TypeToken<Response>() {}.getType();
Response responseobj= new Gson().fromJson(responsestring, objtype);
use Typetoken to parse Gson Object back to original one
您的 Response
表示在名为 map
的字段中包含 HashMap
的对象,但您的 JSON 仅表示 Map
。你不需要有一个封闭的对象,直接反序列化 HashMap
--
HashMap<String, ArrayList<ArrayList<String>>> map;
Type mapType = new TypeToken<HashMap<String, ArrayList<ArrayList<String>>>>() {}.getType();
map = gson.fromJson(response, mapType);
我建议使用以下代码:
String jsonString = "{\n" +
" \"Structure1\": [\n" +
" [\n" +
" \"StringValue1\",\n" +
" \"StringValue2\"\n" +
" ],\n" +
" [\n" +
" \"StringValue1\",\n" +
" \"StringValue2\"\n" +
" ]\n" +
" ],\n" +
" \"Structure2\": [\n" +
" [\n" +
" \"StringValue1\",\n" +
" \"StringValue2\"\n" +
" ]\n" +
" ],\n" +
" \"Structure3\": [\n" +
" [\n" +
" \"StringValue1\",\n" +
" \"StringValue2\"\n" +
" ]\n" +
" ]\n" +
" }";
Map<String, ArrayList<String>> myMap = gson.fromJson(jsonString, HashMap.class);
调试截图如下:
希望对您有所帮助!
我已经尝试 http://www.jsonschema2pojo.org/ 来解决这个问题,但没有成功。我正在尝试为此 Json 格式创建 Json 响应 class:
{
"Structure1": [
[
"StringValue1",
"StringValue2"
],
[
"StringValue1",
"StringValue2"
]
],
"Structure2": [
[
"StringValue1",
"StringValue2"
]
],
"Structure3": [
[
"StringValue1",
"StringValue2"
]
]
}
这是我当前的 class 的样子:
public class Response {
private HashMap<String, ArrayList<ArrayList<String>>> map = new HashMap<String, ArrayList<ArrayList<String>>>();
public HashMap<String, ArrayList<ArrayList<String>>> getMap() {
return map;
}
public void setFile11Txt(HashMap<String, ArrayList<ArrayList<String>>> map) {
this.map = map;
}
}
解析我在做
Response response = gson.fromJson(response, Response.class);
返回的地图最后是空的,我做错了什么?
TypeToken objtype= new TypeToken<Response>() {}.getType();
Response responseobj= new Gson().fromJson(responsestring, objtype);
use Typetoken to parse Gson Object back to original one
您的 Response
表示在名为 map
的字段中包含 HashMap
的对象,但您的 JSON 仅表示 Map
。你不需要有一个封闭的对象,直接反序列化 HashMap
--
HashMap<String, ArrayList<ArrayList<String>>> map;
Type mapType = new TypeToken<HashMap<String, ArrayList<ArrayList<String>>>>() {}.getType();
map = gson.fromJson(response, mapType);
我建议使用以下代码:
String jsonString = "{\n" +
" \"Structure1\": [\n" +
" [\n" +
" \"StringValue1\",\n" +
" \"StringValue2\"\n" +
" ],\n" +
" [\n" +
" \"StringValue1\",\n" +
" \"StringValue2\"\n" +
" ]\n" +
" ],\n" +
" \"Structure2\": [\n" +
" [\n" +
" \"StringValue1\",\n" +
" \"StringValue2\"\n" +
" ]\n" +
" ],\n" +
" \"Structure3\": [\n" +
" [\n" +
" \"StringValue1\",\n" +
" \"StringValue2\"\n" +
" ]\n" +
" ]\n" +
" }";
Map<String, ArrayList<String>> myMap = gson.fromJson(jsonString, HashMap.class);
调试截图如下:
希望对您有所帮助!