JSON中非参数化数组内的对象类型 [英] Type of objects inside non-parameterized arrays in JSON
问题描述
我使用的语言是objective-c,但是这个问题讨论的问题并没有涉及到到这个语言。
我的问题是,如何知道放入一个json数组中的对象的类型,该数组将被映射到非参数化(aka非通用)列表
我发现两个Json Java库无法解决此问题, Jakson 和 Gson ,下面是示例:
import java.io.Serializable;
import java.util.List;
import com.google.gson.Gson;
public class Main
{
public static void main(String [] args)throws Exception
{
Gson g = new Gson ();
Office o = g.fromJson(
{\empx \:\1 \,\emps \:[{\firstName\ :\ Muhammad\,\ lastName\:\ Abdullah\},{\ firstName\:\ XX\,\ lastName\ :null}]}
,Office.class); $(b)System.out.println(((Employee)o.getEmps()。get(0))。getFirstName());
}
}
班级办公室
{
私人列表emps;
private String empx;
public String getEmpx()
{
return empx;
}
public void setEmpx(String empx)
{
this.empx = empx;
}
public List getEmps()
{
return emps;
}
public void setEmps(List emps)
{
this.emps = emps;
}
}
类Employee实现Serializable
{
private static final long serialVersionUID = 1L;
字符串firstName;
字符串姓氏;
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
在 Google Gson code>他们认为这个数组的对象是
java.lang.Object
类型的对象:
线程main中的异常java.lang.ClassCastException:java.lang.Object
但是Jaskon更聪明,它认为这个未知
对象是一个Map:
线程main中的异常java.lang.ClassCastException:java.util.LinkedHashMap
<但是,两者都未能检测到对象(我认为这是不可能的!)
因此,在一种不支持参数化类型的语言中(泛型),是不是有什么办法可以做到这一点? 我会建议使用字典。关键键可以推断出类型,或者可以有一个类型键。
I am trying to write simple function that converts Json strings into Objects.
The language I am using is objective-c however this question discusses issues doesn't relate to this lang.
My question is, How to know the Type of Objects that laid inside a json array that is to be mapped into non-parameterized (a.k.a non-generic) lists??
I found two Json Java libraries unable to solve this issue, Jakson and Gson and here's the example:
import java.io.Serializable;
import java.util.List;
import com.google.gson.Gson;
public class Main
{
public static void main(String[] args) throws Exception
{
Gson g = new Gson();
Office o = g.fromJson(
"{\"empx\":\"1\",\"emps\":[{\"firstName\":\"Muhammad\",\"lastName\":\"Abdullah\"},{\"firstName\":\"XX\",\"lastName\":null}]}"
, Office.class);
System.out.println(((Employee)o.getEmps().get(0)).getFirstName());
}
}
class Office
{
private List emps;
private String empx;
public String getEmpx()
{
return empx;
}
public void setEmpx(String empx)
{
this.empx = empx;
}
public List getEmps()
{
return emps;
}
public void setEmps(List emps)
{
this.emps = emps;
}
}
class Employee implements Serializable
{
private static final long serialVersionUID = 1L;
String firstName;
String lastName;
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
}
In Google Gson
they considered this array's objects as objects of type java.lang.Object
:
Exception in thread "main" java.lang.ClassCastException: java.lang.Object
But Jaskon was much smarter, it considered this unknown
object to be a Map:
Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap
However, Both have been failed to detect the Object (which I think is impossible!)
So, In a language that doesn't support Parameterized types (Generics), Is n't any way to accomplish this?
I would suggest using dictionaries. The key keys could infer the types or there could be a type key.
这篇关于JSON中非参数化数组内的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!