JsonMappingException: No suitable constructor found

后端 未结 4 818
既然无缘
既然无缘 2020-12-18 08:34

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

com.fasterxml.jackson.databind.JsonMappingException: No

相关标签:
4条回答
  • 2020-12-18 09:17

    Your JSON file is not valid. A free online JSON validator : http://jsonlint.com/

    0 讨论(0)
  • 2020-12-18 09:24

    Add a constructor in line of:

    public BlogPost(String author,String title) {
        this.author=author;
        this.title=title;
    }
    

    However it is also possible you need to redefine BlogPost to:

    class BLogPost {
      public BlogPost() { }
      String postId;
      List<SomeObject> someObject;
      Getters/setters
    }
    

    In which SomeObject is your current BLogPost class.

    0 讨论(0)
  • 2020-12-18 09:26

    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 {
    
    0 讨论(0)
  • 2020-12-18 09:31

    Firebase uses -JRHTHaIs-jNPLXOQivY as the key to your object

    {
     author: "gracehop"
     title: "Announcing COBOL, a New Programming Language"
    }
    

    You have to use this as the key to acces the object. You will have to send the correct request to the api. In this case it will be

    https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY

    If you want to receive an array from Firebase you'll have to store it like this.

    posts{
         "1":{
              author: "gracehop"
              title: "Announcing COBOL, a New Programming Language"
            },
         "2":{
             author: "alanisawesome"
             title: "The Turing Machine"
            }
    }
    

    Firebase has no native support for arrays. If you store an array, it really gets stored as an “object” with integers as the key names.

    Check out this Firebase blog post for more information

    0 讨论(0)
提交回复
热议问题