Recyclerview is displaying entire json array instead of object

Asked

Viewed 54 times

0

Inside recyclerView everything is being displayed correctly except the "name" (Notifier > name)

When I use String creatorName = hit.getString("notifier"); it shows the entire "Notifier" object. When I use the String creatorName = hit.getString("name"); nothing is displayed.

I’m still beginner in the development world, so I don’t even know how to pick up an object from within an array of objects, if someone can give me a help I’ll be eternally grateful.

My endpoint returns me the following json:

{
"notifications":[
     { "notifier":{

 "name":"Bruno Silva"
     },
"type_text":"Te deu catch",
"avatar":"https://test.sfo2.digitaloceanspaces.com/cae31d028e5df74_avatar.jpg",
"time_text":"14:58"
}
                ]
}

And this is my mainactivity:

package com.app.contatinhos;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.app.contatinhos.adapter.ExampleAdapter;
import com.app.contatinhos.model.ExampleItem;

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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private ExampleAdapter mExampleAdapter;
    private ArrayList<ExampleItem> mExampleList;
    private RequestQueue mRequestQueue;
    private String notifier;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_four);

        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mExampleList = new ArrayList<>();

        mRequestQueue = Volley.newRequestQueue(this);
        parseJSON();
    }

    private void parseJSON() {
        String url = "https://myserver.com/api/general-data?access_token=cd7e5c9535f8167d9d47084cb5a8e6a767fdf29be5877a10532417db0c060c8517cfc5f265311178068004fef17595cd";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("notifications");


                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject hit = jsonArray.getJSONObject(i);





                                String imageUrl = hit.getString("avatar");
                                String typeText = hit.getString("type_text");



                                String creatorName = hit.getString("notifier");

                                mExampleList.add(new ExampleItem(imageUrl, creatorName, typeText));
                            }



                            mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
                            mRecyclerView.setAdapter(mExampleAdapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        mRequestQueue.add(request);
    }
}

1 answer

0


Use the method getJSONObject("notifier"); to take the internal object and then use the getString("name"); to get the name of the Notifier object, see code below:

JSONObject notifierObject = hit.getJSONObject("notifier");
String creatorName = notifierObject.getString("name");

mExampleList.add(new ExampleItem(imageUrl, creatorName, typeText));

Any questions, you can consult the documentation available at: https://developer.android.com/reference/org/json/JSONObject.html#getJSONObject(java.lang.String)

  • 1

    I had already succeeded, and as incredible as it seems I even used the "notifierObject" as you. But thank you very much, I know that this answer will help someone in the future who just like me had no idea how to pick up one object inside another. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.