Import json file from Firebase Admin SDK

廉价感情. 提交于 2019-12-02 11:31:38

问题


I can update the whole database (Real Database) by importing a json file in Firebase Console:

How can I do it programmatically from server side (with Firebase Admin)?

I tried

private void uploadFirebaseDatabaseFile(JsonObject jsonObject) {
    // As an admin, the app has access to read and write all data, regardless of Security Rules
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    ref.setValue(jsonObject, (error, ref1) -> System.out.println(error + " " + ref1));
}

But it throws java.lang.RuntimeException: java.lang.reflect.InvocationTargetException ... Caused by: java.lang.IllegalStateException: Not a JSON Primitive: { ...


回答1:


Solved it:

private void uploadFirebaseDatabaseFile(Gson gson, JsonObject jsonObject) throws Exception {
    // As an admin, the app has access to read and write all data, regardless of Security Rules
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    CountDownLatch done = new CountDownLatch(1);
    // we must convert our JsonObject and its all nested children to Map<String, Object>
    ref.setValue(gson.fromJson(jsonObject, Map.class), (error, ref1) -> {
            System.out.println(error + " " + ref1);
        done.countDown();
    });
    done.await();
}


来源:https://stackoverflow.com/questions/56401731/import-json-file-from-firebase-admin-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!