如何处理从Firebase返回null的某些值时的情况?

时间:2016-03-31 12:01:59

标签: android json jackson firebase

我使用Firebase为我的应用程序而且我有一个错误。我从Firebase采用的Json看起来像这样:

    "post": {
    "title": "title",
    "date": 1459423087916,
    "comments": [
      {
        "text": "sometext"
      },
      {
        "text": "sometext"
      }
    ]
  }

但有时它看起来像这样:

    "post": {
    "title": "title",
    "date": 1459423087916
  }

我总是接受这个错误:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

有没有办法处理它?<​​/ p>

我的对象模型类:

public class Post {

    private String title;

    private long date;

    private List<Comment> comments;

    // constructor, getters and setters
}

2 个答案:

答案 0 :(得分:1)

我无法通过这种简单的方法重现问题:

public static class Comment {
    public String text;
    public String toString() { return text; }
}
public static class Post {
    private String title;
    private long date;
    private List<Comment> comments;

    public String getTitle() { return title; }
    public long getDate() { return date; }
    public List<Comment> getComments() { return comments; }
    public String toString() {
        return "title="+title+" date="+date+" comments="+(comments!=null?comments.toString():"<null>");
    }
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final TextView view = new TextView(this);
    setContentView(view);

    Firebase.setAndroidContext(this);
    Firebase ref = new Firebase("https://stackoverflow.firebaseio.com/36332909");

    ref.addChildEventListener(new ChildEventListener() {
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            view.append(dataSnapshot.getValue(Post.class).toString()+"\n");
        }

        public void onChildChanged(DataSnapshot dataSnapshot, String s) { }
        public void onChildRemoved(DataSnapshot dataSnapshot) { }
        public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
        public void onCancelled(FirebaseError firebaseError) { }
    });
}

这给了我这个输出:

Activity on Android that shows the loaded items

我还在此处将其添加到我的Android应用中:https://github.com/puf/firebase-stackoverflow-android。有关活动的完整代码,请参阅https://github.com/puf/firebase-stackoverflow-android/blob/master/app/src/main/java/com/firebasedemo/stackoverflow/Activity36332909.java

答案 1 :(得分:0)

你的json对象无效,他们没有外花括号{"post": { }}但我只是假设你只向我们展示了json的某些部分。如果json字符串中没有此类字段,则默认的Jackson行为是将private List<Comment> comments;设置为null。这是演示:

public class Main5 {
private static final String jsonA = " {\n" +
        "    \"title\": \"title\",\n" +
        "    \"date\": 1459423087916,\n" +
        "    \"comments\": [\n" +
        "      {\n" +
        "        \"text\": \"sometext\"\n" +
        "      },\n" +
        "      {\n" +
        "        \"text\": \"sometext\"\n" +
        "      }\n" +
        "    ]\n" +
        "  }";

private static final String jsonB = " {\n" +
        "    \"title\": \"title\",\n" +
        "    \"date\": 1459423087916\n" +
        "  }";


public static void main(String[] args) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    Post tl = mapper.readValue(jsonA, Post.class);
    System.out.println(tl);

    Post t2 = mapper.readValue(jsonB, Post.class);
    System.out.println(t2);
}

public static class Post {
    private String title;
    private long date;
    private List<Comment> comments;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public long getDate() {
        return date;
    }

    public void setDate(long date) {
        this.date = date;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    @Override
    public String toString() {
        return "Post{" +
                "title='" + title + '\'' +
                ", date=" + date +
                ", comments=" + comments +
                '}';
    }

}
public static class Comment {
    private String text;

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public String toString() {
        return "Comment{" +
                "text='" + text + '\'' +
                '}';
    }
}
}

所以我认为你必须修复你的json,或者你有注释来阻止默认行为。