how to get nested data in json with getters and setters

1

I have the following Json:

"matrix": {
"id": 5926,
"name": "matriz fransico zarco",
"friendly_url": "matriz-fransico-zarco",
"headquarter": true,
"user_id": null,
extra_services": [
  0{
"id": 5,
"service_name": "Presupuesto Gratis",
"created_at": "2016-05-18T12:08:12.000-05:00",
"updated_at": "2016-05-18T12:08:12.000-05:00",
"icon": ""
}],

link json capture and I want to access what would be extra services and get service_name will bring me what would be the fields of id, name, friendly_url that are direct in matrix but I have no idea how to do it when they are nested, thank you very much for your help

EDIT

Currently I get what I want through a model class where I have getters and setters of what I want Business.java

public class Business {

    private Integer id, subcategory_id, business_package_id,rating;
    private String name, description, email, website, logo_url_string, icon_default,business_name,icon,cover_default,cover_url_string,extra_services;
    private Boolean status;


    public Business(){}

    public Business(Integer id, Boolean status, Integer subcategory_id, Integer business_package_id,Integer rating,String business_name, String name, String description, String email, String website, String logo_url_string, String cover_default, String icon, String icon_default,String cover_url_string,String extra_services) {

        this.id = id;
        this.icon=icon;
        this.status = status;
        this.subcategory_id = subcategory_id;
        this.business_package_id = business_package_id;
        this.name = name;
        this.business_name=business_name;
        this.description = description;
        this.email = email;
        this.website = website;
        this.logo_url_string = logo_url_string;
        this.rating=rating;
        this.icon_default=icon_default;
        this.cover_default=cover_default;
        this.extra_services=extra_services;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getRating() {
        return rating;
    }

    public void setRating(Integer rating) {
        this.rating = rating;
    }


    public String getBusiness_name() {
        return business_name;
    }

I do not put all the code because that is all, that is, they are pure getters and setters. After those getters I send them to call in an adapter and the adapter I put it in recyclerview that's why I could not put all the code, I do it this way

 holder.mTitle.setText(businessList.get(position).get_name());

where holder is a ViewHolder and I want to put the text of what has the adapter in the position and tell it to get the name that's what I want to do but then I do not know why it is nested service_name I hope it is a bit clearer and the json copy it as an http requester is an api, not I put it complete because it is very long, thank you very much for your help: D

    
asked by Heber Solis 08.04.2017 в 21:35
source

1 answer

-1

You should check the .json to determine if it is a Json object or a Json Array:

  

If the .json starts with { it is considered as Json object.

You use the class JSONObject to get the object:

JSONObject jsonObject = new JSONObject(contenidoJSON);
  

If the .json starts with [ it is considered as Json Arrangement.

Therefore you would use class JSONArray :

 JSONArray jsonArray = new JSONArray(contenidoJSON);
    
answered by 08.04.2017 в 22:59