How to resolve complex JSON and put it in list into list using Retrofit











up vote
0
down vote

favorite












I have JSON as shown below



{
"status" : "succesfull",
"items": [
{
"id" : "1",
"name": "apple",
"counrty": "USA"
},
{
"id": "2",
"name": "banana",
"country": "Jamaica"
},
{
"id":"3",
"name": "potatoes",
"counrty": "Belarus"
},
...
]
}



Let's say, country is varied, and I don't know which countries will be in list




I need to make a list as below:



enter image description here




Any solutions will be appreciated




public class Response {
String status;
ArrayList<Items> items;
}



public class Items{
String id;
String name;
String country;
}









share|improve this question






















  • You'll have to write some code to do the structure conversion.
    – Henry
    Nov 18 at 9:00










  • I don't know how to create list in the list
    – Just Ahead
    Nov 18 at 9:01










  • @JustAhead have you tried expandable listview - androidhive.info/2013/07/android-expandable-list-view-tutorial
    – Navneet Krishna
    Nov 18 at 10:15

















up vote
0
down vote

favorite












I have JSON as shown below



{
"status" : "succesfull",
"items": [
{
"id" : "1",
"name": "apple",
"counrty": "USA"
},
{
"id": "2",
"name": "banana",
"country": "Jamaica"
},
{
"id":"3",
"name": "potatoes",
"counrty": "Belarus"
},
...
]
}



Let's say, country is varied, and I don't know which countries will be in list




I need to make a list as below:



enter image description here




Any solutions will be appreciated




public class Response {
String status;
ArrayList<Items> items;
}



public class Items{
String id;
String name;
String country;
}









share|improve this question






















  • You'll have to write some code to do the structure conversion.
    – Henry
    Nov 18 at 9:00










  • I don't know how to create list in the list
    – Just Ahead
    Nov 18 at 9:01










  • @JustAhead have you tried expandable listview - androidhive.info/2013/07/android-expandable-list-view-tutorial
    – Navneet Krishna
    Nov 18 at 10:15















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have JSON as shown below



{
"status" : "succesfull",
"items": [
{
"id" : "1",
"name": "apple",
"counrty": "USA"
},
{
"id": "2",
"name": "banana",
"country": "Jamaica"
},
{
"id":"3",
"name": "potatoes",
"counrty": "Belarus"
},
...
]
}



Let's say, country is varied, and I don't know which countries will be in list




I need to make a list as below:



enter image description here




Any solutions will be appreciated




public class Response {
String status;
ArrayList<Items> items;
}



public class Items{
String id;
String name;
String country;
}









share|improve this question













I have JSON as shown below



{
"status" : "succesfull",
"items": [
{
"id" : "1",
"name": "apple",
"counrty": "USA"
},
{
"id": "2",
"name": "banana",
"country": "Jamaica"
},
{
"id":"3",
"name": "potatoes",
"counrty": "Belarus"
},
...
]
}



Let's say, country is varied, and I don't know which countries will be in list




I need to make a list as below:



enter image description here




Any solutions will be appreciated




public class Response {
String status;
ArrayList<Items> items;
}



public class Items{
String id;
String name;
String country;
}






java android json android-studio retrofit2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 at 8:57









Just Ahead

648




648












  • You'll have to write some code to do the structure conversion.
    – Henry
    Nov 18 at 9:00










  • I don't know how to create list in the list
    – Just Ahead
    Nov 18 at 9:01










  • @JustAhead have you tried expandable listview - androidhive.info/2013/07/android-expandable-list-view-tutorial
    – Navneet Krishna
    Nov 18 at 10:15




















  • You'll have to write some code to do the structure conversion.
    – Henry
    Nov 18 at 9:00










  • I don't know how to create list in the list
    – Just Ahead
    Nov 18 at 9:01










  • @JustAhead have you tried expandable listview - androidhive.info/2013/07/android-expandable-list-view-tutorial
    – Navneet Krishna
    Nov 18 at 10:15


















You'll have to write some code to do the structure conversion.
– Henry
Nov 18 at 9:00




You'll have to write some code to do the structure conversion.
– Henry
Nov 18 at 9:00












I don't know how to create list in the list
– Just Ahead
Nov 18 at 9:01




I don't know how to create list in the list
– Just Ahead
Nov 18 at 9:01












@JustAhead have you tried expandable listview - androidhive.info/2013/07/android-expandable-list-view-tutorial
– Navneet Krishna
Nov 18 at 10:15






@JustAhead have you tried expandable listview - androidhive.info/2013/07/android-expandable-list-view-tutorial
– Navneet Krishna
Nov 18 at 10:15














2 Answers
2






active

oldest

votes

















up vote
0
down vote













You can do it this way, (sorry for bad formatting)
Make sure to add getter/setter methods in your structure class.



JSONObject jsonObj = new JSONObject(responseString);
Response response = new Response();

if(jsonObj.has("status")){
response.setStatus(jsonObj.getString("status"));
}

if(jsonObj.has("items")) {
JSONArray array = jsonObj.getJSONArray("items");
ArrayList<Items> list =new ArrayList();

for(i=0;i<array.length;i++){
JSONObject json = array.get(i);
Items item = new Items();
item.setId(json.getString("id"));
item.setName(json.getString("name"));
item.setCountry(json.getString("country"));
list.add(item);
}
}
response.setItems(list);





share|improve this answer



















  • 1




    Is there any specific reason for not suggesting JSON parsing libraries like Gson, Jackson or Moshi in your answer? I'm not telling your procedure is wrong or shouldn't be followed but it makes code very long and tomorrow if we want to change any key in JSON structure this code style make developer's life worse. :)
    – Shashanth
    Nov 19 at 3:47










  • The disadvantage of using library is when you apply proguard rules and want to obfuscate your code, these classes will be excluded as the proguard rules for these library may be different, which itself is a huge reason for security purpose.
    – Kinjal Rathod
    Nov 19 at 5:03




















up vote
0
down vote













Hi Just Ahead,



Put your JSON to any JSON parser like here http://www.jsonschema2pojo.org/

Than you need to select Parcelable / Serializable and you will get automatically generated POJO classes.

Than you have to set up retrofit instance with okHttp / gson, create dao interface where you implement your all methods to call your JSON.






share|improve this answer








New contributor




Wiktor Kalinowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359259%2fhow-to-resolve-complex-json-and-put-it-in-list-into-list-using-retrofit%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You can do it this way, (sorry for bad formatting)
    Make sure to add getter/setter methods in your structure class.



    JSONObject jsonObj = new JSONObject(responseString);
    Response response = new Response();

    if(jsonObj.has("status")){
    response.setStatus(jsonObj.getString("status"));
    }

    if(jsonObj.has("items")) {
    JSONArray array = jsonObj.getJSONArray("items");
    ArrayList<Items> list =new ArrayList();

    for(i=0;i<array.length;i++){
    JSONObject json = array.get(i);
    Items item = new Items();
    item.setId(json.getString("id"));
    item.setName(json.getString("name"));
    item.setCountry(json.getString("country"));
    list.add(item);
    }
    }
    response.setItems(list);





    share|improve this answer



















    • 1




      Is there any specific reason for not suggesting JSON parsing libraries like Gson, Jackson or Moshi in your answer? I'm not telling your procedure is wrong or shouldn't be followed but it makes code very long and tomorrow if we want to change any key in JSON structure this code style make developer's life worse. :)
      – Shashanth
      Nov 19 at 3:47










    • The disadvantage of using library is when you apply proguard rules and want to obfuscate your code, these classes will be excluded as the proguard rules for these library may be different, which itself is a huge reason for security purpose.
      – Kinjal Rathod
      Nov 19 at 5:03

















    up vote
    0
    down vote













    You can do it this way, (sorry for bad formatting)
    Make sure to add getter/setter methods in your structure class.



    JSONObject jsonObj = new JSONObject(responseString);
    Response response = new Response();

    if(jsonObj.has("status")){
    response.setStatus(jsonObj.getString("status"));
    }

    if(jsonObj.has("items")) {
    JSONArray array = jsonObj.getJSONArray("items");
    ArrayList<Items> list =new ArrayList();

    for(i=0;i<array.length;i++){
    JSONObject json = array.get(i);
    Items item = new Items();
    item.setId(json.getString("id"));
    item.setName(json.getString("name"));
    item.setCountry(json.getString("country"));
    list.add(item);
    }
    }
    response.setItems(list);





    share|improve this answer



















    • 1




      Is there any specific reason for not suggesting JSON parsing libraries like Gson, Jackson or Moshi in your answer? I'm not telling your procedure is wrong or shouldn't be followed but it makes code very long and tomorrow if we want to change any key in JSON structure this code style make developer's life worse. :)
      – Shashanth
      Nov 19 at 3:47










    • The disadvantage of using library is when you apply proguard rules and want to obfuscate your code, these classes will be excluded as the proguard rules for these library may be different, which itself is a huge reason for security purpose.
      – Kinjal Rathod
      Nov 19 at 5:03















    up vote
    0
    down vote










    up vote
    0
    down vote









    You can do it this way, (sorry for bad formatting)
    Make sure to add getter/setter methods in your structure class.



    JSONObject jsonObj = new JSONObject(responseString);
    Response response = new Response();

    if(jsonObj.has("status")){
    response.setStatus(jsonObj.getString("status"));
    }

    if(jsonObj.has("items")) {
    JSONArray array = jsonObj.getJSONArray("items");
    ArrayList<Items> list =new ArrayList();

    for(i=0;i<array.length;i++){
    JSONObject json = array.get(i);
    Items item = new Items();
    item.setId(json.getString("id"));
    item.setName(json.getString("name"));
    item.setCountry(json.getString("country"));
    list.add(item);
    }
    }
    response.setItems(list);





    share|improve this answer














    You can do it this way, (sorry for bad formatting)
    Make sure to add getter/setter methods in your structure class.



    JSONObject jsonObj = new JSONObject(responseString);
    Response response = new Response();

    if(jsonObj.has("status")){
    response.setStatus(jsonObj.getString("status"));
    }

    if(jsonObj.has("items")) {
    JSONArray array = jsonObj.getJSONArray("items");
    ArrayList<Items> list =new ArrayList();

    for(i=0;i<array.length;i++){
    JSONObject json = array.get(i);
    Items item = new Items();
    item.setId(json.getString("id"));
    item.setName(json.getString("name"));
    item.setCountry(json.getString("country"));
    list.add(item);
    }
    }
    response.setItems(list);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 18 at 10:10

























    answered Nov 18 at 10:03









    Kinjal Rathod

    33125




    33125








    • 1




      Is there any specific reason for not suggesting JSON parsing libraries like Gson, Jackson or Moshi in your answer? I'm not telling your procedure is wrong or shouldn't be followed but it makes code very long and tomorrow if we want to change any key in JSON structure this code style make developer's life worse. :)
      – Shashanth
      Nov 19 at 3:47










    • The disadvantage of using library is when you apply proguard rules and want to obfuscate your code, these classes will be excluded as the proguard rules for these library may be different, which itself is a huge reason for security purpose.
      – Kinjal Rathod
      Nov 19 at 5:03
















    • 1




      Is there any specific reason for not suggesting JSON parsing libraries like Gson, Jackson or Moshi in your answer? I'm not telling your procedure is wrong or shouldn't be followed but it makes code very long and tomorrow if we want to change any key in JSON structure this code style make developer's life worse. :)
      – Shashanth
      Nov 19 at 3:47










    • The disadvantage of using library is when you apply proguard rules and want to obfuscate your code, these classes will be excluded as the proguard rules for these library may be different, which itself is a huge reason for security purpose.
      – Kinjal Rathod
      Nov 19 at 5:03










    1




    1




    Is there any specific reason for not suggesting JSON parsing libraries like Gson, Jackson or Moshi in your answer? I'm not telling your procedure is wrong or shouldn't be followed but it makes code very long and tomorrow if we want to change any key in JSON structure this code style make developer's life worse. :)
    – Shashanth
    Nov 19 at 3:47




    Is there any specific reason for not suggesting JSON parsing libraries like Gson, Jackson or Moshi in your answer? I'm not telling your procedure is wrong or shouldn't be followed but it makes code very long and tomorrow if we want to change any key in JSON structure this code style make developer's life worse. :)
    – Shashanth
    Nov 19 at 3:47












    The disadvantage of using library is when you apply proguard rules and want to obfuscate your code, these classes will be excluded as the proguard rules for these library may be different, which itself is a huge reason for security purpose.
    – Kinjal Rathod
    Nov 19 at 5:03






    The disadvantage of using library is when you apply proguard rules and want to obfuscate your code, these classes will be excluded as the proguard rules for these library may be different, which itself is a huge reason for security purpose.
    – Kinjal Rathod
    Nov 19 at 5:03














    up vote
    0
    down vote













    Hi Just Ahead,



    Put your JSON to any JSON parser like here http://www.jsonschema2pojo.org/

    Than you need to select Parcelable / Serializable and you will get automatically generated POJO classes.

    Than you have to set up retrofit instance with okHttp / gson, create dao interface where you implement your all methods to call your JSON.






    share|improve this answer








    New contributor




    Wiktor Kalinowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote













      Hi Just Ahead,



      Put your JSON to any JSON parser like here http://www.jsonschema2pojo.org/

      Than you need to select Parcelable / Serializable and you will get automatically generated POJO classes.

      Than you have to set up retrofit instance with okHttp / gson, create dao interface where you implement your all methods to call your JSON.






      share|improve this answer








      New contributor




      Wiktor Kalinowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        0
        down vote










        up vote
        0
        down vote









        Hi Just Ahead,



        Put your JSON to any JSON parser like here http://www.jsonschema2pojo.org/

        Than you need to select Parcelable / Serializable and you will get automatically generated POJO classes.

        Than you have to set up retrofit instance with okHttp / gson, create dao interface where you implement your all methods to call your JSON.






        share|improve this answer








        New contributor




        Wiktor Kalinowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        Hi Just Ahead,



        Put your JSON to any JSON parser like here http://www.jsonschema2pojo.org/

        Than you need to select Parcelable / Serializable and you will get automatically generated POJO classes.

        Than you have to set up retrofit instance with okHttp / gson, create dao interface where you implement your all methods to call your JSON.







        share|improve this answer








        New contributor




        Wiktor Kalinowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Wiktor Kalinowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered Nov 18 at 10:45









        Wiktor Kalinowski

        112




        112




        New contributor




        Wiktor Kalinowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Wiktor Kalinowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Wiktor Kalinowski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359259%2fhow-to-resolve-complex-json-and-put-it-in-list-into-list-using-retrofit%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin