How do you treat Json's answer properly?



  • введите сюда кодThat's what I'm sending to the server:

    "device_id":"a40d38a959f1ca65"
    

    That's the answer that should be:

        "personnel":[
          {
             "personnel_number":444,
             "first_name":"Иван",
             "last_name":"mts",
             "middle_name":"",
             "id":1012651
          },
          {
             "personnel_number":777,
             "first_name":"Василий",
             "last_name":"mts1",
             "middle_name":"",
             "id":1012652
          }
       ],
       "success":true
    

    List<Post> posts = Collections.singletonList(response.body());
    Iterator itr = posts.iterator();

                while(itr.hasNext()) {
                    post = (Post) itr.next();
                    Log.e("asd", "yes " + post.getId());
                }
    



  • Why do you have a model in front of the mass? @SerializedName("personnel_number")I think you should also consider that there are objects that you don't process. By logic, you need something like that:

    class PersonnelArray{
    
    @SerializedName("personnel")
    private List&lt;PersonnelValue&gt; personnels;
    
    public List&lt;PersonnelValue&gt; getPersonnels() {
        return personnels;
    }
    

    }

    and human model:

    class PersonnelValue{
    @SerializedName("personnel_number")
    @Expose
    private String perNum;
    ....

    public void setPersonnelNumber(Integer personnelNumber) {
          this.personnelNumber = personnelNumber;
     }
    
    
    public Integer getPersonnelNumber() {
       return personnelNumber;
     }
    

    }

    And you've all been in one class model, which is a little against your json. Next, you can get a list and go through the cycle. https://dds861.medium.com/json-arrays-parsing-using-retrofit-and-recycleview-9b2d494cc24c I hope you need to work with json masses.

    UPDATE

    This is how the cycle goes:

    List myEmpls = new ArrayList();
    Iterator itr = myEmpls.iterator();

    while(itr.hasNext()) {
    PersonnelValue pobj = (PersonnelValue) itr.next();
    System.out.println(pobj .getPersonnelNumber());
    }

    P.S. The mete android-studio should be used in matters directly related to IDE. Please consider this in further matters.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2