Java 如何从JSON中正确读取这些泛型?
我对我的第一个泛型容器相当有信心,但仍停留在如何在客户端对铸造进行描述上。这就是我参与学习Java 如何从JSON中正确读取这些泛型?,java,json,generics,libgdx,Java,Json,Generics,Libgdx,我对我的第一个泛型容器相当有信心,但仍停留在如何在客户端对铸造进行描述上。这就是我参与学习之前的工作: CommonNounContainer typeContainer = new Json().fromJson(CommonNounContainer.class, result); 我在考虑为每个类创建不同的容器,但这似乎不是一个好的设计。以下是我在新的泛型容器中进行的更新的非工作读取尝试: JSONContainer<CommonNoun> typeContainer = n
之前的工作:
CommonNounContainer typeContainer = new Json().fromJson(CommonNounContainer.class, result);
我在考虑为每个类创建不同的容器,但这似乎不是一个好的设计。以下是我在新的泛型容器中进行的更新的非工作读取尝试:
JSONContainer<CommonNoun> typeContainer = new Json().fromJson(JSONContainer.class, result);
我肯定有一种方法,我应该在等号右边包含一个对普通名词类型的引用,但我还没弄明白。我该怎么做?关于泛型、强制转换、JSON和剥离类信息,有很多适用的帖子。我尝试遵循的其中一个方法与上面的转换无关,它认为在构建过程中将t类作为私有变量添加到容器中:
但是我在试图正确引用该类时遇到了类似的语法问题,只是在过程中的不同位置。我也怀疑,在告诉JSON如何对文件中的信息进行分类之前,我是否可以从JSON文件中读取这个类变量
:
类型参数:
参数:
如果类型未知,则类型可能为null。
json
返回:
可能为空。
我可能已经有了一个由deduper提交的可行答案,但根据要求,以下是CommonNounonContainer和JSONContainer类:
import java.util.ArrayList;
public class CommonNounContainer {
private CommonNoun myCommonNoun;
private ArrayList<CommonNounContainer> children;
public CommonNounContainer(CommonNoun concept) {
myCommonNoun = concept;
children = new ArrayList<CommonNounContainer>();
}
//Creates an empty shell. This would be for categories you want to group by, but not display/select in the select box.
public CommonNounContainer() {
children = new ArrayList<CommonNounContainer>();
}
public void addChildren(ArrayList<CommonNounContainer> newChildren) {
children.addAll(newChildren);
}
public void addChild(CommonNoun concept) {
children.add(new CommonNounContainer(concept));
}
public ArrayList<CommonNounContainer> getChildren() {
return children;
}
public CommonNoun getValue() {
return myCommonNoun;
}
public boolean hasChildren() {
if (children.size() > 0) return true;
else return false;
}
public String toString() {
return myCommonNoun.toString();
}
}
public class JSONContainer<T> {
private T myObject;
private ArrayList<JSONContainer<T>> children;
// public Class<T> typeParameterClass;
public JSONContainer() {
}
public JSONContainer(T anObject) {
myObject = anObject;
children = new ArrayList<JSONContainer<T>>();
}
/* public JSONContainer(T anObject, Class<T> typeParameterClass) {
myObject = anObject;
children = new ArrayList<JSONContainer<T>>();
this.typeParameterClass = typeParameterClass;
}
*/
public void addChildren(ArrayList<JSONContainer<T>> newChildren) {
children.addAll(newChildren);
}
public void addChild(T concept) {
children.add(new JSONContainer<T>(concept));
}
public ArrayList<JSONContainer<T>> getChildren() {
return children;
}
public T getValue() {
return myObject;
}
public boolean hasChildren() {
if (children.size() > 0) return true;
else return false;
}
public String toString() {
return myObject.toString();
}
}
import java.util.ArrayList;
公共类公共容器{
私有普通名词;
私人ArrayList儿童;
公共CommonNon容器(CommonNon概念){
名词=概念;
children=newarraylist();
}
//创建一个空壳。这将用于您要按其分组的类别,但不用于在“选择”框中显示/选择。
公共容器(){
children=newarraylist();
}
public void addChildren(ArrayList newChildren){
children.addAll(newChildren);
}
public void addChild(CommonNoun概念){
添加(新容器(概念));
}
公共ArrayList getChildren(){
返回儿童;
}
公共CommonNoun getValue(){
返回名词;
}
公共布尔hasChildren(){
if(children.size()>0)返回true;
否则返回false;
}
公共字符串toString(){
返回myCommonNoun.toString();
}
}
公共类JSONContainer{
私有T对象;
私人ArrayList儿童;
//公共类typeParameterClass;
公共JSONContainer(){
}
公共JSONContainer(T对象){
myObject=一个对象;
children=newarraylist();
}
/*公共JSONContainer(T对象,类类型参数类){
myObject=一个对象;
children=newarraylist();
this.typeParameterClass=typeParameterClass;
}
*/
public void addChildren(ArrayList newChildren){
children.addAll(newChildren);
}
公共儿童(T概念){
添加(新JSONContainer(概念));
}
公共ArrayList getChildren(){
返回儿童;
}
公共T getValue(){
返回myObject;
}
公共布尔hasChildren(){
if(children.size()>0)返回true;
否则返回false;
}
公共字符串toString(){
返回myObject.toString();
}
}
要求增加的课程:
public class CommonNoun extends Concept {
/**
*
*/
private static final long serialVersionUID = 6444629581712454049L;
public CommonNoun() {
super();
}
public CommonNoun(String name, ConceptID cidIn) {
super(name, cidIn);
this.form = ConceptDefs.COMMON_NOUN;
}
}
public class Concept implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2561549161503772431L;
private ConceptID cid = null;
private final String name;
Integer form = 0;
// ArrayList<ProperRelationship> myRelationships = null;
/* @Deprecated
public Concept(String name) {
this.name = name;
}*/
public Concept() {
name = "";
}
public Concept(String name, ConceptID cidIn) {
// this(name);
this.name = name;
cid = cidIn;
}
/*
* This should be over-ridden by any subclasses
*/
public Integer getForm() {
return form;
}
public ConceptID getID() {
return cid;
}
public void setID(ConceptID cidIn) {
cid = cidIn;
}
//this doesn't make any sense. Throw exception?
public String getName() {
return name;
}
public boolean isCommon() {
return true;
}
/**
*
* @return
*/
@Override
public String toString() {
return getName() + "(" + cid.toString() + ")";
}
public boolean equals(Concept other) {
return ((getID().equals(other.getID())));
}
}
public class ConceptID implements Serializable {
long oid;
public ConceptID() {
oid = -1;
}
public ConceptID(long oid) {
this.oid = oid;
}
public long getValue() {
return oid;
}
/**
*
* @return
*/
@Override
public String toString() {
return Long.toString(oid);
}
public Long toLong() {
return Long.valueOf(oid);
}
public boolean equals(ConceptID other) {
return (oid == other.getValue());
}
/**
* Factory model for generating ConceptIDs
*
* This one is here as a convenience as many IDs come in as a String from web POSTs
* @param idAsString
* @return
*/
static public ConceptID parseIntoID(String idAsString) {
ConceptID returnID = null;
try {
returnID = new ConceptID( Long.parseLong(idAsString) );
} catch (Exception e) {
System.err.println("Expected the string, " + idAsString + ", to be Long parsable.");
e.printStackTrace();
}
return returnID;
}
public类CommonNoun扩展了概念{
/**
*
*/
私有静态最终长serialVersionUID=6444629581712454049L;
公共名词{
超级();
}
public CommonNoun(字符串名,ConceptID cidIn){
super(名称,cidIn);
this.form=ConceptDefs.COMMON_名词;
}
}
公共类概念实现了可序列化{
/**
*
*/
私有静态最终长serialVersionUID=2561549161503772431L;
私有概念ID cid=null;
私有最终字符串名;
整数形式=0;
//ArrayList myRelationships=null;
/*@弃用
公共概念(字符串名称){
this.name=名称;
}*/
公共概念(){
name=“”;
}
公共概念(字符串名称,概念ID cidIn){
//这个(名字),;
this.name=名称;
cid=cidIn;
}
/*
*这应该被任何子类所超越
*/
公共整数getForm(){
申报表;
}
公共概念ID getID(){
返回cid;
}
public void setID(ConceptID cidIn){
cid=cidIn;
}
//这没有任何意义。抛出异常?
公共字符串getName(){
返回名称;
}
公共布尔值isCommon(){
返回true;
}
/**
*
*@返回
*/
@凌驾
公共字符串toString(){
返回getName()+”(“+cid.toString()+”);
}
公共布尔等于(概念其他){
return((getID().equals)(other.getID());
}
}
公共类ConceptID实现了可序列化{
长圆形;
公共概念ID(){
oid=-1;
}
公共概念ID(长oid){
this.oid=oid;
}
公共长getValue(){
返回oid;
}
/**
*
*@返回
*/
@凌驾
公共字符串toString(){
返回Long.toString(oid);
}
公营长龙({
返回Long.valueOf(oid);
}
公共布尔等于(概念ID其他){
返回(oid==other.getValue());
}
/**
*用于生成概念ID的工厂模型
*
*这一个是为了方便,因为许多ID都是以字符串形式从web帖子中输入的
*@param-idAsString
*@返回
*/
静态公共概念ID parseIntoID(字符串idAsString){
ConceptID returnID=null;
尝试
import java.util.ArrayList;
public class CommonNounContainer {
private CommonNoun myCommonNoun;
private ArrayList<CommonNounContainer> children;
public CommonNounContainer(CommonNoun concept) {
myCommonNoun = concept;
children = new ArrayList<CommonNounContainer>();
}
//Creates an empty shell. This would be for categories you want to group by, but not display/select in the select box.
public CommonNounContainer() {
children = new ArrayList<CommonNounContainer>();
}
public void addChildren(ArrayList<CommonNounContainer> newChildren) {
children.addAll(newChildren);
}
public void addChild(CommonNoun concept) {
children.add(new CommonNounContainer(concept));
}
public ArrayList<CommonNounContainer> getChildren() {
return children;
}
public CommonNoun getValue() {
return myCommonNoun;
}
public boolean hasChildren() {
if (children.size() > 0) return true;
else return false;
}
public String toString() {
return myCommonNoun.toString();
}
}
public class JSONContainer<T> {
private T myObject;
private ArrayList<JSONContainer<T>> children;
// public Class<T> typeParameterClass;
public JSONContainer() {
}
public JSONContainer(T anObject) {
myObject = anObject;
children = new ArrayList<JSONContainer<T>>();
}
/* public JSONContainer(T anObject, Class<T> typeParameterClass) {
myObject = anObject;
children = new ArrayList<JSONContainer<T>>();
this.typeParameterClass = typeParameterClass;
}
*/
public void addChildren(ArrayList<JSONContainer<T>> newChildren) {
children.addAll(newChildren);
}
public void addChild(T concept) {
children.add(new JSONContainer<T>(concept));
}
public ArrayList<JSONContainer<T>> getChildren() {
return children;
}
public T getValue() {
return myObject;
}
public boolean hasChildren() {
if (children.size() > 0) return true;
else return false;
}
public String toString() {
return myObject.toString();
}
}
public class CommonNoun extends Concept {
/**
*
*/
private static final long serialVersionUID = 6444629581712454049L;
public CommonNoun() {
super();
}
public CommonNoun(String name, ConceptID cidIn) {
super(name, cidIn);
this.form = ConceptDefs.COMMON_NOUN;
}
}
public class Concept implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2561549161503772431L;
private ConceptID cid = null;
private final String name;
Integer form = 0;
// ArrayList<ProperRelationship> myRelationships = null;
/* @Deprecated
public Concept(String name) {
this.name = name;
}*/
public Concept() {
name = "";
}
public Concept(String name, ConceptID cidIn) {
// this(name);
this.name = name;
cid = cidIn;
}
/*
* This should be over-ridden by any subclasses
*/
public Integer getForm() {
return form;
}
public ConceptID getID() {
return cid;
}
public void setID(ConceptID cidIn) {
cid = cidIn;
}
//this doesn't make any sense. Throw exception?
public String getName() {
return name;
}
public boolean isCommon() {
return true;
}
/**
*
* @return
*/
@Override
public String toString() {
return getName() + "(" + cid.toString() + ")";
}
public boolean equals(Concept other) {
return ((getID().equals(other.getID())));
}
}
public class ConceptID implements Serializable {
long oid;
public ConceptID() {
oid = -1;
}
public ConceptID(long oid) {
this.oid = oid;
}
public long getValue() {
return oid;
}
/**
*
* @return
*/
@Override
public String toString() {
return Long.toString(oid);
}
public Long toLong() {
return Long.valueOf(oid);
}
public boolean equals(ConceptID other) {
return (oid == other.getValue());
}
/**
* Factory model for generating ConceptIDs
*
* This one is here as a convenience as many IDs come in as a String from web POSTs
* @param idAsString
* @return
*/
static public ConceptID parseIntoID(String idAsString) {
ConceptID returnID = null;
try {
returnID = new ConceptID( Long.parseLong(idAsString) );
} catch (Exception e) {
System.err.println("Expected the string, " + idAsString + ", to be Long parsable.");
e.printStackTrace();
}
return returnID;
}
Type safety: The expression of type JSONContainer needs unchecked conversion to conform to JSONContainer
...Communicator.java uses unchecked or unsafe operations.
...Recompile with -Xlint:unchecked for details.
result = {"myObject":{"cid":{"oid":129},"name":"technology","form":1},"children":[]}
com.badlogic.gdx.utils.SerializationException: Field not found: cid (java.lang.Object)...
...
private static final String JADEN_AS_JSON = "{jden:{class:CommonNoun,person:Jaden,place:Hollywood,thing:HashBeen}}";
private static final String JADEN_FAILS_AS_ACTOR = "{jden:{class:CommonNoun,person:Jaden,place:Hollywood,thing:HasBeen, cid:{oid:129} }}";
static public void main( String ... args ){
out.printf( "%1$22s%n", "foo");
JSONContainer< CommonNoun > wtf = new JSONContainer< > ( );
CommonNoun wtBrattyF = new CommonNoun( "Jaden Pinkett Smith", "Hollywood", "HasBeen" );
wtf.setJden( wtBrattyF );
out.printf( "%1$42s%n", wtf );
Json jden = new Json();
out.printf("%1$59s%n", jden.toJson( wtf ) );
JSONContainer wtReifiableF = jden.fromJson(JSONContainer.class, JADEN_AS_JSON); /* This is fine */
out.printf("%1$59s%n", jden.toJson( wtReifiableF ) );
JSONContainer/*< CommonNoun >*/ wtUnReifiableF = jden.fromJson( JSONContainer.class, JADEN_AS_JSON );
wtUnReifiableF = jden.fromJson( JSONContainer.class, JADEN_FAILS_AS_ACTOR ); /* This causes the error you reported */
}
...
JSONContainer [ jden: CommonNoun [ person: Jaden Pinkett Smith, place: Hollywood, thing: HasBeen ] ]
{jden:{class:CommonNoun,person:Jaden Pinkett Smith,place:Hollywood,thing:HasBeen}}
{jden:{class:CommonNoun,person:Jaden,place:Hollywood,thing:HashBeen}}
Exception in thread "main" com.badlogic.gdx.utils.SerializationException: Field not found: cid (CommonNoun)
Serialization trace:
{}.jden.cid
jden (JSONContainer)
at com.badlogic.gdx.utils.Json.readFields(Json.java:893)
at com.badlogic.gdx.utils.Json.readValue(Json.java:1074)
at com.badlogic.gdx.utils.Json.readFields(Json.java:902)
at com.badlogic.gdx.utils.Json.readValue(Json.java:1074)
at com.badlogic.gdx.utils.Json.fromJson(Json.java:829)
at DeduperAnswer.main(DeduperAnswer.java:33)
public class Cid {
int oid;
/* ... getter and setter elided ... */
}
public class CommonNoun {
Cid cid;
String name;
int form;
/* ... getters and setters elided ... */
}
result = {"myObject":{"cid":{"oid":129},"name":"technology","form":1},"children":[]}
{myObject:{class:CommonNoun,cid:{oid:139},name:Jada Pinkett Smith,form:69},children:[{myObject:{class:CommonNoun,cid:{oid:666},name:Jaden Pinkett Smith,form:-666},children:[]},{myObject:{class:CommonNoun,cid:{oid:69},name:Willow Pinkett Smith,form:69},children:[]}]}