Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.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 如何识别Jackson错误中缺少的类型id?_Java_Json_Jackson_Deserialization_Json Deserialization - Fatal编程技术网

Java 如何识别Jackson错误中缺少的类型id?

Java 如何识别Jackson错误中缺少的类型id?,java,json,jackson,deserialization,json-deserialization,Java,Json,Jackson,Deserialization,Json Deserialization,我使用Jackson将JSON写入文本文件,JSON表示从抽象类继承的2个类,但无论是否同时使用这两个或其中一个/或类,都会发生错误。JSON似乎编写正确,但在阅读时,我发现以下错误: Missing type id when trying to resolve subtype of [simple type, class model.BaseContact]: missing type id property 'type' at [Source: (File); line: 52,

我使用Jackson将JSON写入文本文件,JSON表示从抽象类继承的2个类,但无论是否同时使用这两个或其中一个/或类,都会发生错误。JSON似乎编写正确,但在阅读时,我发现以下错误:

Missing type id when trying to resolve subtype of [simple type, class model.BaseContact]: missing type id property 'type'
     at [Source: (File); line: 52, column: 1]
json as follows:
    {
   "allContacts" : [ {
     "type" : "personal",
    "addressCity" : "Hamilton",
    "addressNum" : "199",
   "addressPOBox" : null,
    "addressPostCode" : null,
    "addressStreet" : "River Rd",
    "addressSuburb" : null,
    "email" : null,
    "latitude" : null,
    "longitude" : null,
    "name" : "silly simon",
    "notes" : null,
    "phoneNumber" : "09754321",
    "photoBytes" : null,
    "photoURL" : null
  }, {
    "type" : "personal",
    "addressCity" : "Auckland",
    "addressNum" : "482",
    "addressPOBox" : null,
    "addressPostCode" : null,
    "addressStreet" : "Smith Rd",
    "addressSuburb" : null,
    "email" : null,
    "latitude" : null,
    "longitude" : null,
    "name" : "paul smith",
    "notes" : null,
    "phoneNumber" : "0544555",
    "photoBytes" : null,
    "photoURL" : null
  }, {
    "type" : "personal",
    "addressCity" : "Appleby",
    "addressNum" : "123",
    "addressPOBox" : null,
    "addressPostCode" : null,
    "addressStreet" : "Apple rd",
    "addressSuburb" : null,
    "email" : null,
    "latitude" : null,
    "name" : "Steve Jobbs",
    "notes" : null,
    "phoneNumber" : "08004343",
    "photoBytes" : null,
    "photoURL" : null
  } ],
  "size" : 3
}
错误消息引用第52行第1列,假设调试器从第1行开始,该行将是最后一个大括号之后的行

BaseContact
类标题如下:

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(
        use=JsonTypeInfo.Id.NAME,
        include=JsonTypeInfo.As.PROPERTY,
        property="type")
@JsonSubTypes({
        @JsonSubTypes.Type(value=PersonContact.class, name= "personal"),
        @JsonSubTypes.Type(value= BusinessContact.class, name="business")
})

public  abstract class BaseContact {

public String name;
public String addressNum;
public String addressStreet;
public String addressSuburb;
public String addressCity;
public String addressPOBox;
public String addressPostCode;
public Double latitude;
public Double longitude;

public String photoURL;
public String photoBytes;
public String phoneNumber;
public String email;

public String notes;

public BaseContact() {
    //DEFAULT CONSTRUCTOR

}


    public BaseContact( String name, String addressNum, String addressStreet, 
    String addressCity, String phoneNumber) {

    this.name = name;
    this.addressNum = addressNum;
    this.addressStreet = addressStreet;
    this.addressCity = addressCity;
    this.phoneNumber = phoneNumber;
}
 public BusinessService readAllData(String fn) {
  ArrayList<BaseContact> abl = new ArrayList<BaseContact>();
            try {
                abl = new ObjectMapper().readerFor(BaseContact.class).readValue(new File(fn));
                Log.d("qq","abl"+ abl);
            } catch (IOException e) {
                Log.d("qq", "failed reading " + e.getMessage().toString());
                e.printStackTrace();
            }


             BusinessService b = new BusinessService();
            return b;
    }
package model;

import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(
    use=JsonTypeInfo.Id.NAME,
    include=JsonTypeInfo.As.PROPERTY,
    property="type")
@JsonTypeName("type")
public class BusinessContact extends BaseContact {
public String companyName;
public String websiteURL;
public String businessHours;
//def constructor
public BusinessContact(){
};
public BusinessContact(String name,  String addressNum, String addressStreet, 
String addressCity, String phoneNumber, String companyName, String websiteURL, String businessHours) {
    super(name, addressNum, addressStreet, addressCity, phoneNumber);
    this.companyName = companyName;
    this.websiteURL = websiteURL;
    this.businessHours = businessHours;
}

//Getters and setters
public String getCompanyName() {
    return companyName;
}
public void setCompanyName(String companyName) {
    this.companyName = companyName;
}
public String getWebsiteURL() {
    return websiteURL;
}
public void setWebsiteURL(String websiteURL) {
    this.websiteURL = websiteURL;
}
public String getBusinessHours() {
    return businessHours;
}
public void setBusinessHours(String businessHours) {
    this.businessHours = businessHours;
}
public String visitWebsite(int i ){
    //get website, construct intent
    return"url intent";
}
public Boolean isOpen(int i ){
    //do math for day and time and return true if open
    return true;
}
@Override
public String toString() {
    String output= this.getClass() + "name: "+ this.name + " " + "company"+ this.companyName + "Hours "+ this.businessHours + "Website "+ this.websiteURL+ " address: " + this.addressNum+ " , " + this.addressStreet + " , " + this.addressSuburb+ "," +
            this.addressCity +" , CODE " + this.addressPostCode + " PO BOX " + this.addressPOBox + "PH: " +  this.phoneNumber + "email: " + this.email + "notes: "+ this.notes ;
    return output ;
}
调用函数如下:

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(
        use=JsonTypeInfo.Id.NAME,
        include=JsonTypeInfo.As.PROPERTY,
        property="type")
@JsonSubTypes({
        @JsonSubTypes.Type(value=PersonContact.class, name= "personal"),
        @JsonSubTypes.Type(value= BusinessContact.class, name="business")
})

public  abstract class BaseContact {

public String name;
public String addressNum;
public String addressStreet;
public String addressSuburb;
public String addressCity;
public String addressPOBox;
public String addressPostCode;
public Double latitude;
public Double longitude;

public String photoURL;
public String photoBytes;
public String phoneNumber;
public String email;

public String notes;

public BaseContact() {
    //DEFAULT CONSTRUCTOR

}


    public BaseContact( String name, String addressNum, String addressStreet, 
    String addressCity, String phoneNumber) {

    this.name = name;
    this.addressNum = addressNum;
    this.addressStreet = addressStreet;
    this.addressCity = addressCity;
    this.phoneNumber = phoneNumber;
}
 public BusinessService readAllData(String fn) {
  ArrayList<BaseContact> abl = new ArrayList<BaseContact>();
            try {
                abl = new ObjectMapper().readerFor(BaseContact.class).readValue(new File(fn));
                Log.d("qq","abl"+ abl);
            } catch (IOException e) {
                Log.d("qq", "failed reading " + e.getMessage().toString());
                e.printStackTrace();
            }


             BusinessService b = new BusinessService();
            return b;
    }
package model;

import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(
    use=JsonTypeInfo.Id.NAME,
    include=JsonTypeInfo.As.PROPERTY,
    property="type")
@JsonTypeName("type")
public class BusinessContact extends BaseContact {
public String companyName;
public String websiteURL;
public String businessHours;
//def constructor
public BusinessContact(){
};
public BusinessContact(String name,  String addressNum, String addressStreet, 
String addressCity, String phoneNumber, String companyName, String websiteURL, String businessHours) {
    super(name, addressNum, addressStreet, addressCity, phoneNumber);
    this.companyName = companyName;
    this.websiteURL = websiteURL;
    this.businessHours = businessHours;
}

//Getters and setters
public String getCompanyName() {
    return companyName;
}
public void setCompanyName(String companyName) {
    this.companyName = companyName;
}
public String getWebsiteURL() {
    return websiteURL;
}
public void setWebsiteURL(String websiteURL) {
    this.websiteURL = websiteURL;
}
public String getBusinessHours() {
    return businessHours;
}
public void setBusinessHours(String businessHours) {
    this.businessHours = businessHours;
}
public String visitWebsite(int i ){
    //get website, construct intent
    return"url intent";
}
public Boolean isOpen(int i ){
    //do math for day and time and return true if open
    return true;
}
@Override
public String toString() {
    String output= this.getClass() + "name: "+ this.name + " " + "company"+ this.companyName + "Hours "+ this.businessHours + "Website "+ this.websiteURL+ " address: " + this.addressNum+ " , " + this.addressStreet + " , " + this.addressSuburb+ "," +
            this.addressCity +" , CODE " + this.addressPostCode + " PO BOX " + this.addressPOBox + "PH: " +  this.phoneNumber + "email: " + this.email + "notes: "+ this.notes ;
    return output ;
}
}

PersonContact
类(继承自abstract
BaseContact
):


更新:

BusinessContact
类应该用
@JsonTypeName(“个人”)
而不是
@JsonTypeName(“类型”)
注释,因为您应该在继承器中定义特定类型

注释完全可以从子类中删除

更新2:

另外,
PersonContact
类应具有默认构造函数:

public PersonContact(){}
输入JSON文件不是列表,而是具有两个属性的实体
allContacts
size
。因此,它无法映射到。因此,应创建具有这两个属性的新实体:

public class ContactsWrapper
{
   private List<BaseContact> allContacts;
   private int size;

   public List<BaseContact> getAllContacts()
   {
      return allContacts;
   }

   public void setAllContacts(List<BaseContact> allContacts)
   {
      this.allContacts = allContacts;
   }

   public int getSize()
   {
      return size;
   }

   public void setSize(int size)
   {
      this.size = size;
   }
}

现在JSON被映射到
ContactsRapper
,联系人列表被分配给使用getter的
abl
变量。

添加了继承自abstract BaseContact的BusinessContact和PersonContact类感谢,我已经做了这些更改,但我仍然得到“在尝试解析的子类型时读取缺少的类型id失败”[simple type,class model.BaseContact]:缺少类型id属性“type”“”我已使用其他步骤更新了答案,以解决您的问题。请参阅更新2部分。我无法更新您的答案,但更新2是有效的,非常感谢IgorThe NoArg构造函数部分在这里提供了帮助