JsonMappingException:找不到合适的构造函数 [英] JsonMappingException: No suitable constructor found

查看:31
本文介绍了JsonMappingException:找不到合适的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施他们文档中给出的 firebase 示例.我正面临这个错误:

I am implementing a firebase example as given in their documentations. I am facing this error:

com.fasterxml.jackson.databind.JsonMappingException:找不到适合类型 [简单类型,类 com.XYZ.$BlogPost] 的构造函数:无法从 JSON 对象实例化(需要添加/启用类型信息?)

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.XYZ.$BlogPost]: can not instantiate from JSON object (need to add/enable type information?)

这是我的代码:

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        Firebase.setAndroidContext(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Get a reference to our posts
        Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");
        // Attach an listener to read the data at our posts reference
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                System.out.println("There are " + snapshot.getChildrenCount() + " blog posts");
                for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                    BlogPost post = postSnapshot.getValue(BlogPost.class);
                    System.out.println(post.getAuthor() + " - " + post.getTitle());
                }
            }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }
        });
    }
public class BlogPost {
        private String author;
        private String title;
        public BlogPost() {
            // empty default constructor, necessary for Firebase to be able to deserialize blog posts
        }
        public String getAuthor() {
            return author;
        }
        public String getTitle() {
            return title;
        }
    }
}

我在同一件事上遇到了很多问题,说要包含反序列化 JSON 所需的空构造函数.我已经包括了,但我仍然无法解决这个问题.这是我试图反序列化的 JSON:

I have gone through many questions on the same thing saying to include empty constructor necessary to deserialize the JSON. I have included that but still I am not able to resolve the issue. This is the JSON I am trying to deserialize:

{  
   "-JRHTHaIs-jNPLXOQivY":{  
      "author":"gracehop",
      "title":"Announcing COBOL, a New Programming Language"
   },
   "-JRHTHaKuITFIhnj02kE":{  
      "author":"alanisawesome",
      "title":"The Turing Machine"
   }
}

我不知道我的代码中缺少什么.感谢您对此的任何帮助.

I don't know what I am missing in my code. Any help regrading this is appreciated.

推荐答案

我猜您的 BlogPost 类是您活动的内部类.在这种情况下,Java 会向引用包含对象的每个对象添加一个隐藏字段.而这个隐式字段当然不会出现在您的 JSON 数据中.

I'm guessing your BlogPost class is an inner class of your activity. In that case Java adds a hidden field to each object that refers to the containing object. And this implicit field is of course not present in your JSON data.

要解决此问题,您应该将 BlogPost 类保留在单独的文件中(使其不是内部类)或将其标记为静态类(删除隐式字段):

To solve this, you should either keep the BlogPost class in a separate file (so that it's not an inner class) or mark it as a static class (which removes the implicit field):

public static class BlogPost {

这篇关于JsonMappingException:找不到合适的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆