简体   繁体   English

Gson stackoverflow Gson.fromJson

[英]Gson stackoverflow Gson.fromJson

I am executing a Web Service, and this is my response, bu when I try convert this JSONObject(org.json.JSONObject) to specific object using Gson library, my app crashes. 我正在执行一个Web服务,这是我的回应,当我尝试使用Gson库将此JSONObject(org.json.JSONObject)转换为特定对象时,我的应用程序崩溃了。 So, I don't know why happened this. 所以,我不知道为什么会这样。 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"
}]"
}

Error: 错误:

    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)

When i validate ur given json it have some error (The Example Json give is not a valid json).Your json have " in elementos array before opening and close ,maybe that causing a stack overflow error when you try to parse the json using Gson. 当我验证给你的json它有一些错误(示例Json给出的不是一个有效的json)。你的json在打开和关闭之前有在元素数组中,当你尝试使用Gson解析json时可能导致堆栈溢出错误。

"elementos": "[{.....]" “elementos”:“[{.....]”

Please verfy you json with some json validator site for Example jsonlint 请使用json验证器站点为jsonlint验证json

So first we need to valid json ,i have made ur json vaild by omitting the " 所以首先我们需要有效的json,我已经通过省略 json vaild

it will be like this 它会是这样的

Your Json after the change 改变之后你的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"
    }
]
}

**Now the Coding Part : ** To parse this jsonresponse using Gson please follow the Steps below **现在编码部分:**要使用Gson解析此jsonresponse,请按照以下步骤操作

Make a class 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.


}

Class Atributos Atributos类

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;
}

Class ElementosObj 类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;

}

And now in your main class where you Parse The Above json 而现在在你的主要课程中你解析了上面的json

Main class 主要课程

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);
    }

The Output will be like this 输出将是这样的

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

Hope this will help you Thank you 希望这会对你有所帮助谢谢你

In case anyone else runs into this, the problem in my case was that I needed to replace: 万一其他人遇到这个问题,我的问题是我需要更换:

private final Logger LOGGER = Logger.getLogger(this.getClass().getName());

with

private transient Logger LOGGER = Logger.getLogger(this.getClass().getName());

Note the keyword "transient". 请注意关键字“瞬态”。 This is further explained at https://stackoverflow.com/a/5889590/2308858 . 这将在https://stackoverflow.com/a/5889590/2308858进一步解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM