1

I have been trying to convert a String array from Firebase to an actual array but it comes as a single string from Firebase.

To clarify it, this is what I have on Firebase:

date_image

And I am trying to retrieve those Dates and Convert them to an Array, as they become a single string which makes it very difficult to separate into different dates.

I have tried to replace the quotation of the string as indicatedhere, hoping to get the square brackets only and use that a array, since I'll be having an array[], which I succeeded in doing but converting the array to list shows double square brackets as seen below.

06-22 13:35:18.241 10352-10352/com.angelserve.zacktinga.angelserv I/System.out: LIST ARRAY: [[Sat Jun 30 00:00:00 GMT+02:00 2018, Sat Jun 30 00:00:00 GMT+02:00 2018, Sun Jul 01 00:00:00 GMT+02:00 2018, Mon Jul 02 00:00:00 GMT+02:00 2018, Tue Jul 03 00:00:00 GMT+02:00 2018, Wed Jul 04 00:00:00 GMT+02:00 2018, Thu Jul 05 00:00:00 GMT+02:00 2018, Fri Jul 06 00:00:00 GMT+02:00 2018, Sat Jul 07 00:00:00 GMT+02:00 2018]]

I am getting the dates a DataSnapshot from firebase like below:

mCurrentUserRefDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            System.out.println("DATE LIST: " + dataSnapshot.child("requested_dates").getValue());

            dates.replace("\"", "");

            List<String> list = Arrays.asList(dates);
            System.out.println("LIST ARRAY: " + list);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

What I need as a solution is a way to convert my firebase datasnapshot of

requested_dates

and convert them into a working array.

This is my JSON Firebase datasnapshot: enter image description here

2
  • Please add a more detailed json / screenshot of your database. Commented Jun 22, 2018 at 12:23
  • Thanks Alex, I've just edited it now and included the detailed json of the database. Commented Jun 22, 2018 at 12:28

1 Answer 1

2

To solve this, I recommend you to use the following steps. First you need to get the value of the requested_dates property like this:

String string = dataSnapshot.child("requested_dates").getValue(String.class);

Assuming that you have a [ at the beginning of the String and a ] at the end of the String, let's replace both symbols with an empty String. Then we need split it using a comma and a space ,. The result will be an array of strings.

String[] parts = string.replace("[", "").replace("]", "").split(", ");
for (int i = 0; i < parts.length; i++) {
    Log.d("TAG", parts[i]);
}

The output in your console will be:

Wed Jun 13 00:00:00 GMT+02:00 2018
//and so on
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.