gpt4 book ai didi

java - 从 json 响应调用时 GSON 抛出错误

转载 作者:行者123 更新时间:2023-12-02 04:12:10 24 4
gpt4 key购买 nike

每当我尝试加载 gson.fromJson 命令时,它总是会抛出异常。错误是

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 7 column 2 path

我发送的 JSON 结果是这样的:

{"status":true,"statuscode":200,"message":"List(s) found!","result":[{"id":"1","name":"Afghan (AFGHANISTAN)","country_code":"AF"},{"id":"2","name":"Albanian (ALBANIA)","country_code":"AL"}],"batch":2,"totalrows":2}

这是测试模型:

package com.app.testapp.test.models;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.google.gson.annotations.SerializedName;

public class TestModel extends Model {

@Column(name="id")
@SerializedName("id")
String id;

@Column(name="name")
@SerializedName("name")
String name;

@Column(name="country_code")
@SerializedName("country_code")
String country_code;

public String getTestModelId(){ return id;}

public void setTestModelId(String TestModelId){
this.id = TestModelId;
}

public String getTestModelName(){ return ("name")

public void setTestModelName(String TestModelName){
this.name = TestModelName;
}

public String TestModelCode(){ return country_code;}

public void SetTestModelCode(String TestModelCode){
this.country_code = TestModelCode;
}

@Override
public String toString(){
return "Nationality [NationalityId="+id+", NationalityName="+name+" , " +
"NationalityCode="+country_code+"]";

}
}

这是我的 httpclient 类,一切似乎都运行良好,直到出现错误部分

   Gson gson = new GsonBuilder().create();

Type listType = new TypeToken<List<TestModel>>() {
}.getType();

try {
Log.v("MyTag","Try");

List<TestModel> result = gson.fromJson(new String(responseBody), listType); -- error here

callback.onGetTestSucess(result);
} catch (Exception e) {

callback.onGetTestFailure();

GSON 似乎有什么问题?是因为我没有正确序列化它吗?

最佳答案

您的 JSON 无效

{
"status": true,
"statuscode": 200,
"message": "List(s) found!",
"result": [
{
"id": "1",
"name": "Afghan (AFGHANISTAN)",
"country_code": "AF"
},
{
"id": "2",
"name": "Albanian (ALBANIA)",
"country_code": "AL"
}
],
"batch": 2,
"totalrows"2" //Here is your error fix it to "totalrows": 2
}

关于java - 从 json 响应调用时 GSON 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33689647/

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