有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JSONObject在Firebase中返回null

数据已成功添加到Firebase(通过Firebase控制台确认)。但当我试图提取数据时,得到的是空值

package com.example.shaur.nimblenavigationdrawer;

import 安卓.os.Bundle;
import 安卓.support.v4.app.Fragment;
import 安卓.support.v7.widget.LinearLayoutManager;
import 安卓.support.v7.widget.RecyclerView;
import 安卓.util.Log;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class all_books extends Fragment {

    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference rootRef = db.getReference();
    DatabaseReference bookRef = rootRef.child("BookAds");


    RecyclerView recyclerView;

    List<Books_getter_function> bookList;

    String b_name,b_price,b_desc,negotiate,contact,email;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_all_books, container, false);


        bookList = new ArrayList<>();
        final BookAdapter adapter = new BookAdapter(getActivity(),bookList);

        recyclerView = view.findViewById(R.id.recyclerView_books);
        recyclerView.setHasFixedSize(true);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.setAdapter(adapter);

//This hardcoded data is being displayed correctly.
        //bookList.add(new Books_getter_function("Ghatak 2008","200","test@gmail.com","Excellent book must buy","1233214390",R.drawable.book_blue_icon));  

        bookRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String value = String.valueOf(dataSnapshot.getValue());
                //LOG IS WORKING FINE
                Log.i("Book",value);

                try {
                    JSONObject object = new JSONObject(value);
                    b_name = object.getString("BookName");
                    b_price = object.getString("BookPrice");
                    contact = object.getString("ContactNumber");
                    b_desc  = object.getString("Description");
                    negotiate = object.getString("Negotiable");
                    email = object.getString("Email");

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                bookList.add(new Books_getter_function(b_name,b_price,email,b_desc,contact,R.drawable.book_blue_icon));
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


        return view;
    }
}

日志值正确:{UserId=IVXm0BS3NWZnsp7e6qz0W3bsYz2,BookPrice=122,Description=excellent,ContactNumber=989999999,coverable=true,BookName=HK das,Email=test@gmail.com}

JSONObject传递给字符串b_name、b_price的值为null。为什么会这样? 我已经记录了值字符串,它给了我正确的输出,现在我只需要提取数据

我已经验证了其他一切都是正确的添加硬编码数据,以检查是否得到显示,它的工作完全正常

这里的问题是JSONObject从FireBase提取的数据

OUTPUT


共 (1) 个答案

  1. # 1 楼答案

    在firebase中,数据库由JSON组成,但不需要使用JSONObject来获取数据。你可以简单地这样做,而不是:-

     b_name = object.getString("BookName");
                    b_price = object.getString("BookPrice");
                    contact = object.getString("ContactNumber");
                    b_desc  = object.getString("Description");
                    negotiate = object.getString("Negotiable");
                    email = object.getString("Email");
    

    这样做:

    String b_name=dataSnapshot.child("BookName").getValue().toString();
    String b_price=dataSnapshot.child("BookPrice").getValue().toString();
    String contact=dataSnapshot.child("ContactNumber").getValue().toString();
    String b_desc=dataSnapshot.child("Description").getValue().toString();
    String negotiate=dataSnapshot.child("Negotiable").getValue().toString();
    String email=dataSnapshot.child("Email").getValue().toString();
    

    child()是数据库中的一个属性