-1

I'm trying to get Java Object from JSON string that has inner arrays, there are pretty same questions, but none couldn't solve my problem. Now in console i get MethodPackage.JsonDeserialize@6580cfdd (I'm doing with objectmapper) My aim is to get separately values in json to do some manupulations

below is my full code:

JSONstring:
{
"status": 1,
"message": "ok",
"sheduleCod": "NOST_A_Persons_m_noaccum",
"algorithms": [{
    "cod": "No_st_alg_1",
    "kcp": "U6000427",
    "dtBeg": "2017-11-01 00:00:00",
    "dtEnd": "2017-12-01 00:00:00"
}, {
    "cod": "No_st_alg_2",
    "kcp": "U6000427",
    "dtBeg": "2017-11-01 00:00:00",
    "dtEnd": "2017-12-01 00:00:00"
}, {
    "cod": "No_st_alg_3",
    "kcp": "U6000427",
    "dtBeg": "2017-11-01 00:00:00",
    "dtEnd": "2017-12-01 00:00:00"
}]
}

          Main.class

String jsonString = response.toString();
JsonDeserialize deserialize = objectMapper.readValue(jsonString, JsonDeserialize.class);
System.out.println(deserialize);}

JsonDeserialize.class 
public class JsonDeserialize {
private String status;
private String message;
private String sheduleCod;
private List<Algorithm> algorithms;

            in JsonDeserialize.class 

public class JsonDeserialize {
private String status;
private String message;
private String sheduleCod;
private List<Algorithm> algorithms;
public JsonDeserialize(String status, String message, String sheduleCod, List<Algorithm> algorithms) {
    this.status = status;
    this.message = message;
    this.sheduleCod = sheduleCod;
    this.algorithms = algorithms;
}

..... and then getters and setters

                  Algorithm.class

public class Algorithm {
private String cod;
private String kcp;
private String dtBeg;
private String dtEnd;

public Algorithm(String cod, String kcp, String dtBeg, String dtEnd) {
    this.cod = cod;
    this.kcp = kcp;
    this.dtBeg = dtBeg;
    this.dtEnd = dtEnd;
}
public Algorithm () {

}
Marks
  • 21
  • 1
  • 7

2 Answers2

1

The output MethodPackage.JsonDeserialize@6580cfdd means that you print the reference and not the values of the object.

To fix this problem override the toString method within the JsonDeserialize class like the following:

@Override
public String toString() {
    String values = ""; // you could also use a StringBuilder here
    values += "Status: " + status + "\n";
    values += "Message: " + message + "\n";
    // ....
    return values;
}

or use:

System.out.println(deserialize.getStatus())
System.out.println(deserialize.getMessage());
// ...
wake-0
  • 3,918
  • 5
  • 28
  • 45
  • from [getStatus()] i get [1] in console, bth how can i override [toString] even if i dont use getters for now? – Marks Nov 29 '17 at 06:59
0

If you are using Jackson or GSON, you you can just create POJOs that match the structure of the data and it will work automatically. Also, in those pojos you can either make the names of the properties be exactly the same as the JSON has or else use the jackson annotation to let you provide the JSON property name for each object property. But mainly i don't see any getters and setters for your POJOS that you did show, and most likely you do need those. Be carefeul that you name the properties using the right 'bean naming' conventions.

  • ok. i did getters and setters. but when i try to get value it shows NULL (for example **JsonDeserialize j = new JsonDeserialize(); System.out.println(j.getSheduleCod());**), and the same for array. can't get any array element. – Marks Nov 29 '17 at 09:53
  • Make sure you have a public parameterless constructor, and that your getters and setters are public, and check out @JsonProperty on this page: http://www.baeldung.com/jackson-annotations –  Nov 29 '17 at 16:13