【问题标题】:Firebase Save Data from Comma Separated ValueFirebase 从逗号分隔值中保存数据
【发布时间】:2020-01-08 14:52:21
【问题描述】:

我有以下来自我的 php 的 Json 响应

目前我有以下代码可以将数据插入到 firebase

        JSONArray arr = new JSONArray(result);
        //JSONObject jObj = arr.getJSONObject(0);
        String[] stocks = new String[arr.length()];

        for(int i=0;i<arr.length();i++){

            JSONObject obj = arr.getJSONObject(i);

            mDatabase= FirebaseDatabase.getInstance().getReference().child(context.getResources().getText(R.string.bid_request).toString().trim());

            Map<String, Object> userValues = new HashMap<>();
            DatabaseReference newBid=mDatabase.push();
            userValues.put("uid", obj.getString("user_id"));
            userValues.put("uNme",obj.getString("first_name")+" "+obj.getString("last_name"));
            userValues.put("uMoble", obj.getString("user_mobile"));
            userValues.put("uAvtr",  obj.getString("src"));
            userValues.put("uRat",  obj.getString("user_ratings"));

            userValues.put("pck", obj.getString("pickup_location"));
            userValues.put("drp",obj.getString("drop_location"));
            userValues.put("pLat", obj.getString("pickup_latitude"));
            userValues.put("pLan",  obj.getString("pickup_longitude"));
            userValues.put("dLat", obj.getString("drop_latitude"));
            userValues.put("dLan",  obj.getString("drop_longitude"));
            userValues.put("did",obj.getString("driver_id"));
            userValues.put("dMoble", obj.getString("driver_mobile"));        

            newBid.setValue(userValues, new DatabaseReference.CompletionListener() {
                @Override
                public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
            });

它正在将数据保存为以下 firebase 结构

我想要做的是每个逗号分隔的移动号码从 json 响应将保存为下面的 firebase 结构。我怎样才能做到这一点

【问题讨论】:

    标签: json firebase firebase-realtime-database


    【解决方案1】:

    Json 对象中不能有多个具有相同名称的键,您可以做的一件事是将其存储为 String 的数组列表,同时将数据添加到实时数据库中,将其拆分为 , 为关注

    userValues.put("dMoble", obj.getString("driver_mobile").split(","));
    

    如果您想为每个手机号码输入密钥:- mobilenum 请尝试以下解决方案

                List<HashMap<String ,String>> dMobiles  = new ArrayList<>();
                for (String mobilenum:jsonObject.getString("driver_mobile").split(",")){
                    HashMap<String,String> mobileMap = new HashMap<>();
                    mobileMap.put("mobilenum",mobilenum);
                    dMobiles.add(mobileMap);
                }
                userValues.put("dMoble",dMobiles);
    

    【讨论】:

    • 它给出了错误“不支持序列化数组,请改用列表”如果我以这种方式保存另一点,那么我如何根据 mobile no 对值进行快照,例如 mobileno=mobileno 跨度>
    • 我已经更新了答案,看看这是否适合你
    【解决方案2】:

    在这里,您将整个, 分隔的电话号码作为一个字符串。所以 Firebase 假设它是一个字符串。要单独添加电话号码,您需要划分手机号码字符串并将其存储在数组中。使用foreach 循环到该数组,您可以添加带有索引的单个数字。

    顺便说一句,如果在单个节点中使用相同的键,Firebase 会覆盖该值,因此要分别添加每个手机号码,您需要为每个手机号码设置不同的键值。

    【讨论】:

    • 使用 String[] MobileNumbers = obj.getString("driver_mobile").split(","); 并使用 for (string mobile : MobileNumbers) { //Access your Individual Mobile Number as strings here. //Add each number individually with a different index //Goto the **dmobil** node and add the numbers } 如果有帮助,请点赞!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多