JsonMappingException:没有合适的构造发现 [英] JsonMappingException: No suitable constructor found

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

问题描述

我实施火力例如在他们的单证给出。我面对这个错误:

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

com.fasterxml.jackson.databind.JsonMappingException:找到了类型没有合适的构造函数[简单类型,类com.XYZ $博文]:无法从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?)

下面是我的code:

    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"
   }
}

我不知道我在code很想念。任何帮助改编这是AP preciated。

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

推荐答案

我猜你的博文类的<​​STRONG>内部类您活动。在这种情况下的Java增加了一个隐藏字段是指包含对象的每个对象。这隐含场当然不是present在你的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.

要解决这个问题,就应该把保持博文类在一个单独的文件(这样它不是一个内部类),或者将其标记为一个静态类(这消除隐场):

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天全站免登陆