gpt4 book ai didi

java - Gson stackoverflow Gson.fromJson

转载 作者:行者123 更新时间:2023-12-02 05:52:16 30 4
gpt4 key购买 nike

我正在执行一个 Web 服务,这是我的响应,但是当我尝试使用 Gson 库将此 JSONObject(org.json.JSONObject) 转换为特定对象时,我的应用程序崩溃了。所以,我不知道为什么会发生这种情况。JSON:

{
"atributos": {
"id": "1",
"nombre": "Cliente",
"descripcion": "Cliente",
"version": "1"
},
"elementos": "[{
"id": "1",
"name" : "akira"
},
{
"id": "4",
"name" : "akira"
},
{
"id": "5",
"name" : "akira"
},
{
"id": "6",
"name" : "akira"
},
{
"id": "7",
"name" : "akira"
},
{
"id": "8",
"name" : "akira"
},
{
"id": "9",
"name" : "akira"
},
{
"id": "10",
"name" : "akira"
},
{
"id": "11",
"name" : "akira"
},
{
"id": "12",
"name" : "akira"
},
{
"id": "13",
"name" : "akira"
},
{
"id": "14",
"name" : "akira"
},
{
"id": "15",
"name" : "akira"
},
{
"id": "16",
"name" : "akira"
},
{
"id": "17",
"name" : "akira"
},
{
"id": "18",
"name" : "akira"
}]"
}

错误:

    java.lang.StackOverflowError
05-02 17:50:56.166: E/AndroidRuntime(8130): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:117)
05-02 17:50:56.166: E/AndroidRuntime(8130): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
05-02 17:50:56.166: E/AndroidRuntime(8130): at com.google.gson.Gson.getAdapter(Gson.java:356)
05-02 17:50:56.166: E/AndroidRuntime(8130): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
05-02 17:50:56.166: E/AndroidRuntime(8130): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
05-02 17:50:56.166: E/AndroidRuntime(8130): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
05-02 17:50:56.166: E/AndroidRuntime(8130): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
05-02 17:50:56.166: E/AndroidRuntime(8130): at com.google.gson.Gson.getAdapter(Gson.java:356)
05-02 17:50:56.166: E/AndroidRuntime(8130): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
05-02 17:50:56.166: E/AndroidRuntime(8130): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)

最佳答案

当我验证给定的 json 时,它有一些错误(给出的示例 Json 不是有效的 json)。在打开和关闭之前,您的 json 在 elementos 数组中有 " ,可能会导致堆栈溢出当您尝试使用 Gson 解析 json 时出错。

"elementos": "[{.....]"

请使用一些 json validator 站点验证您的 json,例如 jsonlint

所以首先我们需要有效的 json ,我通过省略 " 使你的 json 有效

会是这样的

更改后的 Json

{
"atributos": {
"id": "1",
"nombre": "Cliente",
"descripcion": "Cliente",
"version": "1"
},
"elementos": [
{
"id": "1",
"name": "akira"
},
{
"id": "4",
"name": "akira"
},
{
"id": "5",
"name": "akira"
},
{
"id": "6",
"name": "akira"
},
{
"id": "7",
"name": "akira"
},
{
"id": "8",
"name": "akira"
},
{
"id": "9",
"name": "akira"
},
{
"id": "10",
"name": "akira"
},
{
"id": "11",
"name": "akira"
},
{
"id": "12",
"name": "akira"
},
{
"id": "13",
"name": "akira"
},
{
"id": "14",
"name": "akira"
},
{
"id": "15",
"name": "akira"
},
{
"id": "16",
"name": "akira"
},
{
"id": "17",
"name": "akira"
},
{
"id": "18",
"name": "akira"
}
]
}

**现在是编码部分:**要使用 Gson 解析此 jsonresponse,请按照以下步骤操作

创建一个 JsonResponse 类

public class JsonResponse {

/*Class handle the Whole json*/

//Variable name should be same as the names given in the json response

Atributos atributos;// Attributos class handles the data in atributos obj from the json . The variable name of Attributos should be same as the json objects ie atributos

ArrayList<ElementosObj>elementos=null;//elementos is a array which hold some datas ,so make a class ElementosObj to handle the data in the elementos array.


}

类别属性

public class Atributos {

/*Class handle the atributos obj from json*/

//Variable name should be same as the names given in the json response
public String id = null;
public String nombre = null;
public String descripcion = null;
public String version = null;
}

ElementosObj 类

public class ElementosObj {

/*Class handle the elementos array from json*/

//Variable name should be same as the names given in the json response

String id=null;
String name=null;

}

现在在你的主类中解析上面的 json

主类

String jsonData = "String jsonData Contain your parsed jsondata ";


Gson mGson = new Gson();
JsonResponse mObj1 = (JsonResponse) mGson.fromJson(jsonData,
JsonResponse.class);

Log.d("Atributos:ID", mObj1.atributos.id);
Log.d("Atributos:DESCRIBTION", mObj1.atributos.descripcion);
Log.d("Atributos:NUMBER", mObj1.atributos.nombre);
Log.d("Atributos:VERSION", mObj1.atributos.version);

for (int i = 0; i < mObj1.elementos.size(); i++) {
Log.d("Elementos:ID For Postion " + i, mObj1.elementos.get(i).id);
Log.d("Elementos:NAME For Postion " + i,
mObj1.elementos.get(i).name);
}

输出将是这样的

05-03 01:32:51.548: D/Atributos:ID: 1
05-03 01:32:51.548: D/Atributos:DESCRIBTION: Cliente
05-03 01:32:51.548: D/Atributos:NUMBER: Cliente
05-03 01:32:51.548: D/Atributos:VERSION: 1
05-03 01:32:51.548: D/Elementos:ID For Postion 0: 1
05-03 01:32:51.548: D/Elementos:NAME For Postion 0: akira
05-03 01:32:51.548: D/Elementos:ID For Postion 1: 4
05-03 01:32:51.558: D/Elementos:NAME For Postion 1: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 2: 5
05-03 01:32:51.558: D/Elementos:NAME For Postion 2: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 3: 6
05-03 01:32:51.558: D/Elementos:NAME For Postion 3: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 4: 7
05-03 01:32:51.558: D/Elementos:NAME For Postion 4: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 5: 8
05-03 01:32:51.558: D/Elementos:NAME For Postion 5: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 6: 9
05-03 01:32:51.558: D/Elementos:NAME For Postion 6: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 7: 10
05-03 01:32:51.558: D/Elementos:NAME For Postion 7: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 8: 11
05-03 01:32:51.558: D/Elementos:NAME For Postion 8: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 9: 12
05-03 01:32:51.558: D/Elementos:NAME For Postion 9: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 10: 13
05-03 01:32:51.558: D/Elementos:NAME For Postion 10: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 11: 14
05-03 01:32:51.558: D/Elementos:NAME For Postion 11: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 12: 15
05-03 01:32:51.558: D/Elementos:NAME For Postion 12: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 13: 16
05-03 01:32:51.558: D/Elementos:NAME For Postion 13: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 14: 17
05-03 01:32:51.558: D/Elementos:NAME For Postion 14: akira
05-03 01:32:51.558: D/Elementos:ID For Postion 15: 18
05-03 01:32:51.558: D/Elementos:NAME For Postion 15: akira

希望这对您有帮助谢谢

关于java - Gson stackoverflow Gson.fromJson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23438174/

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