简体   繁体   中英

How to get array of object of object and their response using setter and getter method

 "Status": "success",
  "data": [
    {
      "lead_id": "13653",
      "bp_id": "5d832cbeb4a36302da065233",
      "BP_consumer_id": 32,
      "agent_id": "56",
      "vertical_stage": "Meeting",
      "bp_vertical": {
        "vertical_name": "Mahesh Tutorials",
        "vertical_url": "http://mteducare.com",
        "category": "Agriculture/Food Processing",
        "sub_category": "Food",
        "isActive": "yes",
        "NPS_rating": [
          {
            "promoter": {
              "1": {
                "relates_to": "5b2b2a28613b6ad01c014a29",
                "reasons": "Great product looks,Good product features,Product fits my need"
              },
              "2": {
                "relates_to": "5b2b2a9a613b6ad01c014a60",
                "reasons": "Value for Money"
              },
              "3": {
                "relates_to": "5b2b2a9a613b6ad01c014a62",
                "reasons": "Informative Advertisement,Informative Web-site,Informative Brochure"
              },
              "4": {
                "relates_to": "5b754201b9bdada1ab2bc940",
                "reasons": "Informative Sales person,Delivery / Installation"
              },
              "5": {
                "relates_to": "5b754201b9bdada1ab2bc942",
                "reasons": "I Love Godrej products,Positive Social Media reviews"
              },
              "6": {
                "relates_to": "5b754201b9bdada1ab2bc944",
                "reasons": "Other Issue"
              },
              "promoter_title": "Thank-you for High Ratings, give us your compliments"
            },
            "passive": {
              "1": {
                "relates_to": "5b2b2a28613b6ad01c014a29",
                "reasons": "Great product looks,Good product features,Product fits my need"
              },
              "2": {
                "relates_to": "5b2b2a9a613b6ad01c014a60",
                "reasons": "Value for Money"
              },
              "3": {
                "relates_to": "5b2b2a9a613b6ad01c014a62",
                "reasons": "Informative Advertisement,Informative Web-site,Informative Brochure"
              },
              "4": {
                "relates_to": "5b754201b9bdada1ab2bc940",
                "reasons": "Informative Sales person,Delivery / Installation"
              },
              "5": {
                "relates_to": "5b754201b9bdada1ab2bc942",
                "reasons": "I Love Godrej products,Positive Social Media reviews"
              },
              "6": {
                "relates_to": "5b754201b9bdada1ab2bc944",
                "reasons": "Other Issue"
              },
              "passive_title": "Thank you for your Ratings, what went well ?"
            },
          }
        ],
        "short_url": "b6zdm32",
        "shared_url": "http://mteducare.com?32"
      },
      "pledge_id": "5d8c9005b4a363703c0d0234",
      "noOfOffersToBeSelected": "3"
    }
  ],
  "code": 200
}

First convert your json to POJO using online converter http://www.jsonschema2pojo.org/

Then in Java use below code change getString('yourkeyValue') keys as per JSON pojo

public void onSuccess(Object result) {
                try {
                    JSONObject jsonObject = new JSONObject(new JSONTokener(result.toString()).nextValue().toString());
                    if (jsonObject.getString("responseCode").equals("00")) {
                        JSONObject jresponseData = new JSONObject(jsonObject.getString("responseData"));

                        if (jresponseData.has("Table1")) {
                            JSONArray jsonArray = new JSONArray(jresponseData.getString("Table1"));
                            for (int i = 0; i < jsonArray.length(); i++) {

                                JSONObject jsonObject1 = jsonArray.getJSONObject(i);

                                MyPojo listObj = new MyPojo();

                                String sName = StringUtils.defaultIfEmpty(jsonObject1.getString("s_Name"), "").replace("null", "");
                                String s_desc = StringUtils.defaultIfEmpty(jsonObject1.getString("s_About"), "").replace("null", "");
                                String simgUrl = StringUtils.defaultIfEmpty(jsonObject1.getString("Image").replace("@", ""), "");
                                String pk_id = StringUtils.defaultIfEmpty(jsonObject1.getString("PK_ID"), "0");

                                listObj.setChannelName(sName);
                                listObj.setS_shortDes(s_desc);
                            }

                        }

                    }
                }
            }

There are multiple ways you can get it from the server using Rxjava and retrofit and volly, - Refer this link to how to use retrofit https://www.androidhive.info/2016/05/android-working-with-retrofit-http-library/

  • you can use DTO generator plugin in the android studio to generate the getters and setters from JSON.

  • or else you can use http://pojo.sodhanalibrary.com/ link to generate the getters and setters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM