Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 谷歌Gson-反序列化列表<;类别>;对象(通用类型)_Java_Json_Generics_Gson - Fatal编程技术网

Java 谷歌Gson-反序列化列表<;类别>;对象(通用类型)

Java 谷歌Gson-反序列化列表<;类别>;对象(通用类型),java,json,generics,gson,Java,Json,Generics,Gson,我想通过Google Gson传输列表对象,但我不知道如何反序列化泛型类型 我在看了(巴卢斯克的回答)之后尝试了什么: 我捕获JsonParseExceptions,并且“result”不为null 我使用调试器检查了listType,得到了以下结果: List<MyClass> mcList = new ArrayList<>(Arrays.asList(mcArray)); 列表类型 args=类型列表 列表=空 resolvedTypes=类型[1] 加载

我想通过Google Gson传输列表对象,但我不知道如何反序列化泛型类型

我在看了(巴卢斯克的回答)之后尝试了什么:

我捕获JsonParseExceptions,并且“result”不为null

我使用调试器检查了listType,得到了以下结果:

List<MyClass> mcList = new ArrayList<>(Arrays.asList(mcArray));
  • 列表类型
    • args=类型列表
      • 列表=空
      • resolvedTypes=类型[1]
    • 加载程序=路径类加载程序
    • ownerType0=null
    • ownerTypeRes=null
    • rawType=Class(java.util.ArrayList)
    • rawTypeName=“java.util.ArrayList”
因此,“getClass”调用似乎没有正常工作。有什么建议吗

编辑2: 我查了一下电话号码。它提到了在将泛型类型解析为Json时应该发生的运行时异常。我做得“不对”(上面没有显示),就像在示例中一样,但根本没有得到那个异常。因此,我按照用户指南中的建议更改了序列化。但是没有帮助

编辑3:
已解决,请参阅下面我的答案。

反序列化泛型集合的方法:

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

...

Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType();
List<YourClass> yourClassList = new Gson().fromJson(jsonArray, listType);
import java.lang.reflect.Type;
导入com.google.gson.reflect.TypeToken;
...

键入listType=new TypeToken。

因为它回答了我的原始问题,我已经接受了doc_180的答案,但是如果有人再次遇到这个问题,我也会回答我问题的第二部分:

我描述的NullPointerError与列表本身无关,而是与它的内容有关


“MyClass”类没有“no args”构造函数,其超类也没有。一旦我向MyClass及其超类添加了一个简单的“MyClass()”构造函数,一切都正常工作,包括doc_180建议的列表序列化和反序列化。

另一种方法是使用数组作为类型,例如:

MyClass[] mcArray = gson.fromJson(jsonString, MyClass[].class);
这样可以避免使用Type对象的所有麻烦,如果确实需要列表,可以通过以下方式将数组转换为列表:

List<MyClass> mcList = Arrays.asList(mcArray);
参考这篇文章。

我有更好的解决办法。下面是list的包装器类,因此包装器可以存储list的确切类型

public class ListOfJson<T> implements ParameterizedType
{
  private Class<?> wrapped;

  public ListOfJson(Class<T> wrapper)
  {
    this.wrapped = wrapper;
  }

  @Override
  public Type[] getActualTypeArguments()
  {
      return new Type[] { wrapped };
  }

  @Override
  public Type getRawType()
  {
    return List.class;
  }

  @Override
  public Type getOwnerType()
  {
    return null;
  }
}
公共类ListOfJson实现参数化类型
{
私有类包装;
公共ListOfJson(类包装器)
{
this.wrapped=包装器;
}
@凌驾
公共类型[]getActualTypeArguments()
{
返回新类型[]{wrapped};
}
@凌驾
公共类型getRawType()
{
返回列表.class;
}
@凌驾
公共类型getOwnerType()
{
返回null;
}
}
然后,代码可以很简单:

public static <T> List<T> toList(String json, Class<T> typeClass)
{
    return sGson.fromJson(json, new ListOfJson<T>(typeClass));
}
公共静态列表列表(字符串json,类typeClass)
{
返回sGson.fromJson(json,新的ListOfJson(typeClass));
}

我想增加一种可能性。如果您不想使用TypeToken并想将json对象数组转换为ArrayList,那么您可以这样进行:

如果您的json结构类似:

{

"results": [
    {
        "a": 100,
        "b": "value1",
        "c": true
    },
    {
        "a": 200,
        "b": "value2",
        "c": false
    },
    {
        "a": 300,
        "b": "value3",
        "c": true
    }
]
public class ClassName implements Parcelable {

    public ArrayList<InnerClassName> results = new ArrayList<InnerClassName>();
    public static class InnerClassName {
        int a;
        String b;
        boolean c;      
    }
}
}

你的班级结构是这样的:

{

"results": [
    {
        "a": 100,
        "b": "value1",
        "c": true
    },
    {
        "a": 200,
        "b": "value2",
        "c": false
    },
    {
        "a": 300,
        "b": "value3",
        "c": true
    }
]
public class ClassName implements Parcelable {

    public ArrayList<InnerClassName> results = new ArrayList<InnerClassName>();
    public static class InnerClassName {
        int a;
        String b;
        boolean c;      
    }
}

现在您可以访问className对象的每个元素。

我喜欢kays1的答案,但我无法实现它。所以我用他的概念建立了自己的版本

public class JsonListHelper{
    public static final <T> List<T> getList(String json) throws Exception {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        Type typeOfList = new TypeToken<List<T>>(){}.getType();
        return gson.fromJson(json, typeOfList);
    }
}
公共类JsonListHelper{
公共静态最终列表getList(字符串json)引发异常{
Gson Gson=new GsonBuilder().setDateFormat(“yyyy-MM-dd HH:MM:ss”).create();
Type typeOfList=new TypeToken(){}.getType();
返回gson.fromJson(json,typeOfList);
}
}
用法:

List<MyClass> MyList= JsonListHelper.getList(jsonArrayString);
List MyList=JsonListHelper.getList(jsonArrayString);

Wep,实现相同结果的另一种方法。我们使用它是为了它的可读性

不要做这个难读的句子:

Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType();
List<YourClass> list = new Gson().fromJson(jsonArray, listType);
Type listType=new-TypeToken(){}.getType();
List List=new Gson().fromJson(jsonArray,listType);
创建扩展对象列表的空类:

public class YourClassList extends ArrayList<YourClass> {}
public类YourClassList扩展了ArrayList{}
并在解析JSON时使用它:

List<YourClass> list = new Gson().fromJson(jsonArray, YourClassList.class);
List List=new Gson().fromJson(jsonArray,YourClassList.class);

这是一个使用动态定义类型的解决方案。诀窍是使用array.newInstance()创建正确类型的数组

jsonlist中的公共静态列表(字符串json,类clazz){
Object[]array=(Object[])java.lang.reflect.array.newInstance(clazz,0);
array=gson.fromJson(json,array.getClass());
列表=新的ArrayList();

对于(int i=0;i在我的案例中@uncaught\u异常的答案不起作用,我必须使用
List.class
而不是
java.lang.reflect.Type

String jsonDuplicatedItems = request.getSession().getAttribute("jsonDuplicatedItems").toString();
List<Map.Entry<Product, Integer>> entries = gson.fromJson(jsonDuplicatedItems, List.class);
String jsonDuplicatedItems=request.getSession().getAttribute(“jsonDuplicatedItems”).toString();
List entries=gson.fromJson(jsondupplicatedItems,List.class);

有关Gson的“类型”类理解,请参阅示例2

示例1:在这个deserilizeResturant中,我们使用Employee[]数组并获取详细信息

public static void deserializeResturant(){

       String empList ="[{\"name\":\"Ram\",\"empId\":1},{\"name\":\"Surya\",\"empId\":2},{\"name\":\"Prasants\",\"empId\":3}]";
       Gson gson = new Gson();
       Employee[] emp = gson.fromJson(empList, Employee[].class);
       int numberOfElementInJson = emp.length();
       System.out.println("Total JSON Elements" + numberOfElementInJson);
       for(Employee e: emp){
           System.out.println(e.getName());
           System.out.println(e.getEmpId());
       }
   }
例2:

//Above deserilizeResturant used Employee[] array but what if we need to use List<Employee>
public static void deserializeResturantUsingList(){

    String empList ="[{\"name\":\"Ram\",\"empId\":1},{\"name\":\"Surya\",\"empId\":2},{\"name\":\"Prasants\",\"empId\":3}]";
    Gson gson = new Gson();

    // Additionally we need to se the Type then only it accepts List<Employee> which we sent here empTypeList
    Type empTypeList = new TypeToken<ArrayList<Employee>>(){}.getType();


    List<Employee> emp = gson.fromJson(empList, empTypeList);
    int numberOfElementInJson = emp.size();
    System.out.println("Total JSON Elements" + numberOfElementInJson);
    for(Employee e: emp){
        System.out.println(e.getName());
        System.out.println(e.getEmpId());
    }
}
//上面的deserilizeResturant使用了Employee[]数组,但如果我们需要使用List呢
public static void反序列化器esturantusinglist(){
字符串empList=“[{\'name\':\'Ram\',\'empId\':1},{\'name\':\'Surya\',\'empId\':2},{\'name\':\'Prasants\',\'empId\':3}];
Gson Gson=新的Gson();
//此外,我们需要se类型,然后只有它接受我们发送到此处的列表empTypeList
类型empTypeList=newTypeToken(){}.getType();
List emp=gson.fromJson(empList,empTypeList);
int numberOfElementInJson=emp.size();
System.out.println(“JSON元素总数”+numberOfElementInJson);
对于(员工e:emp){
System.out.println(e.getName());
System.out.println(e.getEmpId());
}
}
对于Kotlin,只需:

import java.lang.reflect.Type
import com.google.gson.reflect.TypeToken
...
val type = object : TypeToken<List<T>>() {}.type
import java.lang.reflect.Type
导入com.google.gson.reflect.TypeToken
...
val type=object:TypeToken(){}.type
或者,这里有一个有用的函数:

fun <T> typeOfList(): Type {
    return object : TypeToken<List<T>>() {}.type
}
fun typeOfList():类型{
返回对象:TypeToken(){}.type
}
然后,使用
String jsonDuplicatedItems = request.getSession().getAttribute("jsonDuplicatedItems").toString();
List<Map.Entry<Product, Integer>> entries = gson.fromJson(jsonDuplicatedItems, List.class);
public static void deserializeResturant(){

       String empList ="[{\"name\":\"Ram\",\"empId\":1},{\"name\":\"Surya\",\"empId\":2},{\"name\":\"Prasants\",\"empId\":3}]";
       Gson gson = new Gson();
       Employee[] emp = gson.fromJson(empList, Employee[].class);
       int numberOfElementInJson = emp.length();
       System.out.println("Total JSON Elements" + numberOfElementInJson);
       for(Employee e: emp){
           System.out.println(e.getName());
           System.out.println(e.getEmpId());
       }
   }
//Above deserilizeResturant used Employee[] array but what if we need to use List<Employee>
public static void deserializeResturantUsingList(){

    String empList ="[{\"name\":\"Ram\",\"empId\":1},{\"name\":\"Surya\",\"empId\":2},{\"name\":\"Prasants\",\"empId\":3}]";
    Gson gson = new Gson();

    // Additionally we need to se the Type then only it accepts List<Employee> which we sent here empTypeList
    Type empTypeList = new TypeToken<ArrayList<Employee>>(){}.getType();


    List<Employee> emp = gson.fromJson(empList, empTypeList);
    int numberOfElementInJson = emp.size();
    System.out.println("Total JSON Elements" + numberOfElementInJson);
    for(Employee e: emp){
        System.out.println(e.getName());
        System.out.println(e.getEmpId());
    }
}
import java.lang.reflect.Type
import com.google.gson.reflect.TypeToken
...
val type = object : TypeToken<List<T>>() {}.type
fun <T> typeOfList(): Type {
    return object : TypeToken<List<T>>() {}.type
}
val type = typeOfList<YourMagicObject>()
public <T> List<T> getList(String jsonArray, Class<T> clazz) {
    Type typeOfT = TypeToken.getParameterized(List.class, clazz).getType();
    return new Gson().fromJson(jsonArray, typeOfT);
}
String jsonArray = ...
List<User> user = getList(jsonArray, User.class);