0

This is my json

{
    "profile_min_salary": 10000,
    "profile_requirement_checklist": "Excel Aana Chahiye.",
    "tags": "central,12th pass,thoda english,good english,fluent      
         english,male,fresher,female"
}

Here is my Model

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY,
    getterVisibility = JsonAutoDetect.Visibility.NONE,
    setterVisibility = JsonAutoDetect.Visibility.NONE)
    @JsonIgnoreProperties(ignoreUnknown = true)'

public class Job {

    @JsonProperty("profile_min_salary")
    private Integer profileMinSalary;

     @JsonProperty("profile_requirement_checklist")
    private String profileRequirementChecklist;

     @JsonProperty("tags")
    private String tags;

     public String getTags() {
        return tags;
    }

    public Integer getProfileMinSalary() {
        return profileMinSalary;
    }


    public void setProfileMinSalary(Integer profileMinSalary) {
        this.profileMinSalary = profileMinSalary;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }


    public String getProfileRequirementChecklist() {
        return profileRequirementChecklist;
    }

    public void setProfileRequirementChecklist(String profileRequirementChecklist) {
        this.profileRequirementChecklist = profileRequirementChecklist;
    }
}

This is my firebase code snippet

@Override
public void onChildAdded(final DataSnapshot dataSnapshot, String previousChildName) {

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {

                Job job = dataSnapshot.getValue(Job.class);
                doOperationOnJob(job);
            } catch (Exception e) {
                Log.e(TAG, e.getMessage(), e);
                Crashlytics.logException(e);
            }
        }
    }).start();
}

But my class is getting only tags parameter else are null why ??

While if I am writing this way it is working fine

 @Override
 public void onChildAdded(final DataSnapshot dataSnapshot, String previousChildName) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    String jsonData = new ObjectMapper().writeValueAsString(dataSnapshot.getValue());
       Job job= new ObjectMapper().readValue(jsonData , Job.class);
       doOperationOnJob(job);

                } catch (Exception e) {
                    Log.e(TAG, e.getMessage(), e);
                    Crashlytics.logException(e);
                }
            }
        }).start();
    }

What should i change in code to make it work in first scenario ?? I am stuck.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Lokesh Tiwari
  • 10,496
  • 3
  • 36
  • 45

1 Answers1

0

Make the following changes in your model to work with Firebase correctly.

private Integer profile_min_salary;
private String profile_requirement_checklist;
private String tags;

You don't actually need to set the @JsonProperty annotation to work with Firebase if the property names are the same.

So simply your POJO can look like this

public class Job {
    public Integer profile_min_salary;
    public String profile_requirement_checklist;
    public String tags;
}

As you've declared all the variables public, you can get them simply by creating an instance of your Job class (e.g. Job mJob = new Job();) and then mJob.profile_min_salary

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98