从Firebase Admin SDK导入json文件 [英] Import json file from Firebase Admin SDK

查看:198
本文介绍了从Firebase Admin SDK导入json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过在Firebase控制台中导入json文件来更新整个数据库(真实数据库):

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

如何从服务器端(使用Firebase Admin)以编程方式进行操作?

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

我尝试过

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));
}

但是它抛出java.lang.RuntimeException: java.lang.reflect.InvocationTargetException ... Caused by: java.lang.IllegalStateException: Not a JSON Primitive: { ...

推荐答案

已解决:

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();
}

这篇关于从Firebase Admin SDK导入json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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