如何使用 ArrayDeque 从 json 文件中获取一些数据?
How can I get some data frome json file with ArrayDeque?
我尝试像这样从 json 文件中获取一些数据。
[
{
"type": "text",
"content": "test test test",
"time": 100
},
{
"type": "text",
"content": "abcedfg",
"time": 100
},
{
"type": "text",
"content": "some data",
"time": 100
},
{
"type": "text",
"content": "1234567",
"time": 100
}
]
并像这样定义了一个class。
class TextData {
String type;
String content;
long time;
TextData(String type, String content, long time) {
this.type = type;
this.content = content;
this.time = time;
}
}
我尝试使用 GSON 将 json 数据解析为 ArrayDeque<TextData>
像这样
ArrayDeque<TextData> textDatas =
new Gson().fromJson(getJson(fileName), ArrayDeque.class);
方法 getJson
将获得 json 文件的数据 String
.
这是行不通的。我收到此错误。
failed to deserialize json object "" given the type class java.util.ArrayDeque
我该如何解决?
试试这个
Gson gson = new Gson();
Type listType = new TypeToken<List<TextData>>() {
}.getType();
List<TextData> arralist = (List<TextData>) gson.fromJson(response, listType);
for (int i = 0; i < arralist.size(); i++) {
Log.e("Content", arralist.get(i).getContent());
Log.e("type", arralist.get(i).getType());
Log.e("time", arralist.get(i).getTime() + "");
}
model class
public class TextData {
String type;
String content;
long time;
public TextData(String type, String content, long time) {
this.type = type;
this.content = content;
this.time = time;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
}
结果
我尝试像这样从 json 文件中获取一些数据。
[
{
"type": "text",
"content": "test test test",
"time": 100
},
{
"type": "text",
"content": "abcedfg",
"time": 100
},
{
"type": "text",
"content": "some data",
"time": 100
},
{
"type": "text",
"content": "1234567",
"time": 100
}
]
并像这样定义了一个class。
class TextData {
String type;
String content;
long time;
TextData(String type, String content, long time) {
this.type = type;
this.content = content;
this.time = time;
}
}
我尝试使用 GSON 将 json 数据解析为 ArrayDeque<TextData>
像这样
ArrayDeque<TextData> textDatas =
new Gson().fromJson(getJson(fileName), ArrayDeque.class);
方法 getJson
将获得 json 文件的数据 String
.
这是行不通的。我收到此错误。
failed to deserialize json object "" given the type class java.util.ArrayDeque
我该如何解决?
试试这个
Gson gson = new Gson();
Type listType = new TypeToken<List<TextData>>() {
}.getType();
List<TextData> arralist = (List<TextData>) gson.fromJson(response, listType);
for (int i = 0; i < arralist.size(); i++) {
Log.e("Content", arralist.get(i).getContent());
Log.e("type", arralist.get(i).getType());
Log.e("time", arralist.get(i).getTime() + "");
}
model class
public class TextData {
String type;
String content;
long time;
public TextData(String type, String content, long time) {
this.type = type;
this.content = content;
this.time = time;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
}
结果