-3

I have some data provided from an API for example this is the data, this is just a part of the data, but this is the part that I need to access

  "logo_url_string": null,
                "name": "google",
                "category_image": {
                    "id": 24,
                    "cover_url": {
                        "url": "http://",
                        "icon_circle": {
                            "url": "http://"
                        }

The question is how can I access the "url" inside cover_url thats inside category_image?, and so far I've been able to access it from a Model class with setter and getters and an adapter but I don't know how can I access data that is nested with setters and getters I have to do it with setters and getters because I have an adapter class where I get all the data into de cardview and then I load it to a recycler that is in a fragment, please any help would be great Thank you! my model is like this

Business.java

public class Business {

    private String name, description, email, website, logo_url_string,cover_url_string;


    public Business(){}
public class Images{

}

    public Business(String name,  String logo_url_string) {

        this.name = name;

        this.logo_url_string = logo_url_string;


    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getLogo_url_string() {
        return logo_url_string;
    }

    public void setLogo_url_string(String logo_url_string) {
        this.logo_url_string = logo_url_string;
    }

And I access that data with and Adapter

Adapter.java

public class SearchHorizontalAdapter extends RecyclerView.Adapter<SearchHorizontalAdapter.ViewHolder> {

    private ArrayList<Business> premiumsList;

    Bitmap bitmap;
    ImageView logo;
    private Activity activity;
    private int layoutMolde;



    public SearchHorizontalAdapter(Activity activity, ArrayList<Business> list, int layout ) {

        this.activity = activity;
        this.premiumsList = list;
        layoutMolde = layout;
    }

    @Override
    public SearchHorizontalAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_high_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SearchHorizontalAdapter.ViewHolder holder, int position) {
       // holder.mTitle.setText(premiumsList.get(position));
        holder.mTitle.setText(premiumsList.get(position).getName());
        //holder.mImg.setImageURI(Uri.parse(premiumsList.get(position).getLogo_url_string()));


//        if( Glide.with(this.activity).load(premiumsList.get(position).getLogo_url_string())==null) {
//          Glide.with(this.activity).load(premiumsList.get(position).getCategory_image()).into(holder.mImg);
//        }
        Glide.with(this.activity).load(premiumsList.get(position).getLogo_url_string()).into(holder.mImg);
    }

    @Override
    public int getItemCount() {
        return premiumsList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mTitle;
        public ImageView mImg;


        public ViewHolder(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.nom_business);
            mImg= (ImageView) itemView.findViewById(R.id.img_business);

        }


    }


}
Juan P.
  • 7
  • 6
  • This is simple json parsing. Google how to parse json. – Piyush Feb 16 '17 at 05:01
  • even if it is nested? I mean, I have already parsed some data, but I want the one that's nested – Juan P. Feb 16 '17 at 05:03
  • yor Json is wrong please check it first using http://json.parser.online.fr/ – Rajesh Satvara Feb 16 '17 at 05:04
  • the json isn't wrong but this is just a part of all the data, it is way to much to put it here, I just pasted the part that I need :/ – Juan P. Feb 16 '17 at 05:18
  • @JuanP. try the answers – Kiran Benny Joseph Feb 16 '17 at 05:38
  • Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – rohitanand Feb 16 '17 at 06:26
  • I truly appreaciatte all the response thank you all for your help, but, it is not what i'm asking, the problem that I have is that, i'm trying to get that info with setters and getters because I have to insert that into a cardview and then add the adapter to a recylcler, that is the problem I don't know how to access the nested data to do all that :/ – Juan P. Feb 16 '17 at 18:45

3 Answers3

0

Try This,

    try {
        JSONObject objResult = new JSONObject(response);

        String name = objResult.getString("name");
        JSONObject category_image_obj=objResult.getJSONObject("category_image");
        JSONObject cover_url_obj=category_image_obj.getJSONObject("cover_url");
        String url = cover_url_obj.getString("url");

         ArrayList premiumsList =new ArrayList<>();
         premiumsList.add(new Business(name,url);
        //call adapter here


    } catch (JSONException e) {
        e.printStackTrace();
    }
Komal12
  • 3,340
  • 4
  • 16
  • 25
  • hank you but it is not what I'm asking the problem is that I have a cardview adapter where I add the data that I need to show from setters and getters and then I load the adapter into a recyclerview that is in a fragment where the gson is, that's why I need to do it whit setters and getters like the code above, but I don't know how to make the setters and getters for the nested data THANK YOU SO MUCH for your help – Juan P. Feb 16 '17 at 18:51
0

First of all your json is wrong. If your json is like this,

{
"logo_url_string":null,
"name":"google",
"category_image":{
"id":"24",
"cover_url":{
"url":"http://",
"icon_circle":{
"url":"http://"
          }
     }
   }
 }

Try this

try {
            JSONObject ai=new JSONObject(response);
            JSONObject cat_img=ai.getJSONObject("category_image");
            int id=cat_img.getInt("id");  //you can get id here
            JSONObject cat_url=cat_img.getJSONObject("cover_url");
            String url=cat_url.getString("url");  //you can get url from here
            ArrayList<Business> premiumsList=new ArrayList<>();
            premiumsList.add(new Business(cat_img.getString("name"),url)); //add it to aaray
    } catch (JSONException e) {
            e.printStackTrace();
    }

Furthermore you can use this for making getters and setters for nested json.

Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
  • thank you but it is not what I'm asking the problem is that I have a cardview adapter where I add the data that I need to show from setters and getters and then I load the adapter into a recyclerview that is in a fragment where the gson is, that's why I need to do it whit setters and getters like the code above, but I don't know how to make the setters and getters for the nested data THANK YOU SO MUCH for your help – Juan P. Feb 16 '17 at 18:51
0

IF your JSON response as below then you can parse to use below java code.

{
    "contacts": [
        {
                "id": "c200",
                "name": "Android Developer",
                "email": "android.developer@gmail.com",
                "address": "xx-xx-xxxx,x - street, x - country",
                "gender" : "male",
                "phone": {
                    "mobile": "+91 0000000000",
                    "home": "00 000000",
                    "office": "00 000000"
                }
        }
  ]
}

Java Code where you can parse your JSON. I hope you will get your answer.

@Override
protected Void jsonParsing(String jsonString) {


    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
    // Store a json string in string variable
    String jsonStr = jsonString;

    Log.e(TAG, "Response from url: " + jsonStr);

    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            // Getting JSON Array node
            JSONArray contacts = jsonObj.getJSONArray("contacts");

            // looping through All Contacts
            for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);

                String id = c.getString("id");
                String name = c.getString("name");
                String email = c.getString("email");
                String address = c.getString("address");
                String gender = c.getString("gender");

                // Phone node is JSON Object
                JSONObject phone = c.getJSONObject("phone");
                String mobile = phone.getString("mobile");
                String home = phone.getString("home");
                String office = phone.getString("office");

                // tmp hash map for single contact
                HashMap<String, String> contact = new HashMap<>();

                // adding each child node to HashMap key => value
                contact.put("id", id);
                contact.put("name", name);
                contact.put("email", email);
                contact.put("mobile", mobile);

                // adding contact to contact list
                contactList.add(contact);
            }
        } catch (final JSONException e) {
            Log.e(TAG, "Json parsing error: " + e.getMessage());
        }
    } else {
        Log.e(TAG, "Couldn't get json from server.");
    }
}

For more detail about JSON Parsing please read this documentation. Other read this tutorial that how to parse the JSON.

Farmer
  • 4,093
  • 3
  • 23
  • 47
  • thank you but it is not what I'm asking the problem is that I have a cardview adapter where I add the data that I need to show from setters and getters and then I load the adapter into a recyclerview that is in a fragment where the gson is, that's why I need to do it whit setters and getters like the code above, but I don't know how to make the setters and getters for the nested data THANK YOU SO MUCH for your help – Juan P. Feb 16 '17 at 18:49