JuanPa
JuanPa

Reputation: 59

Reading a json using jackson

I'm having trouble reading this json, the code seems to work, but there's 2 problems

  1. It only reads one block of the json, not entirely.
  2. It always has "null" as a value in the properties.

I've been trying to show the json organized in the console, but when i try those 2 things happens.

Sample of the JSON data:

{
  "RestResponse" : {
    "messages" : [ "More webservices are available at http://www.groupkt.com/post/f2129b88/services.htm", "Total [249] records found." ],
    "result" : [ {
      "name" : "Afghanistan",
      "alpha2_code" : "AF",
      "alpha3_code" : "AFG"
    }, {
      "name" : "Åland Islands",
      "alpha2_code" : "AX",
      "alpha3_code" : "ALA"
    }, {
      "name" : "Albania",
      "alpha2_code" : "AL",
      "alpha3_code" : "ALB"
    }, ...
    ]
  }
}

My code:

public class jsonController {

public void run() {
    ObjectMapper mapper = new ObjectMapper();
    try {

        jsonHandler obj = mapper.readValue(new URL("http://services.groupkt.com/country/get/all"), jsonHandler.class);
        //Organized Print
        String organizedprint = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        System.out.println(organizedprint);


    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And in the main i've got

jsonController obj = new jsonController();
    obj.run();

And here's the jsonHandler

@JsonIgnoreProperties(ignoreUnknown=true)
public class jsonHandler {
    private String restResponse;
    private String messages;
    private String result;
    private String name;
    private String alpha2;
    private String alpha3;
}

Any idea what I'm doing wrong?

Upvotes: 1

Views: 362

Answers (2)

Fabien Benoit-Koch
Fabien Benoit-Koch

Reputation: 2841

Okay your mapping class, jsonHandler is wrong. First of all, it should be capitalized correctly (JsonHandler)

Using http://www.jsonschema2pojo.org/ i generated a better model. It's composed of 3 classes. Simply change the package "com.example" to yours.

package com.example;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"RestResponse"
})
public class JsonHandler {

@JsonProperty("RestResponse")
private RestResponse restResponse;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("RestResponse")
public RestResponse getRestResponse() {
return restResponse;
}

@JsonProperty("RestResponse")
public void setRestResponse(RestResponse restResponse) {
this.restResponse = restResponse;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

com.example.RestResponse.java

package com.example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"messages",
"result"
})
public class RestResponse {

@JsonProperty("messages")
private List<String> messages = null;
@JsonProperty("result")
private List<Result> result = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("messages")
public List<String> getMessages() {
return messages;
}

@JsonProperty("messages")
public void setMessages(List<String> messages) {
this.messages = messages;
}

@JsonProperty("result")
public List<Result> getResult() {
return result;
}

@JsonProperty("result")
public void setResult(List<Result> result) {
this.result = result;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

com.example.Result.java

package com.example;

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name",
"alpha2_code",
"alpha3_code"
})
public class Result {

@JsonProperty("name")
private String name;
@JsonProperty("alpha2_code")
private String alpha2Code;
@JsonProperty("alpha3_code")
private String alpha3Code;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("name")
public String getName() {
return name;
}

@JsonProperty("name")
public void setName(String name) {
this.name = name;
}

@JsonProperty("alpha2_code")
public String getAlpha2Code() {
return alpha2Code;
}

@JsonProperty("alpha2_code")
public void setAlpha2Code(String alpha2Code) {
this.alpha2Code = alpha2Code;
}

@JsonProperty("alpha3_code")
public String getAlpha3Code() {
return alpha3Code;
}

@JsonProperty("alpha3_code")
public void setAlpha3Code(String alpha3Code) {
this.alpha3Code = alpha3Code;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

Upvotes: 0

dsh
dsh

Reputation: 12234

You declared your data types incorrectly in your model. Your Java code declares that the data will have a single object containing 6 string attributes. The JSON data provided by the server is not like that at all. For example, messages is a list of strings and result is a list of objects, not a string. You need to declare your Java model accordingly.

For example:

public class jsonHandler
    {
    private RestResponseStructure restResponse;
    }

public class RestResponseStructure
    {
    private List<String> messages;
    private List<CountryRecord> results;
    }

public class CountryRecord {
    private String name;
    private String alpha2_code;
    private String alpha3_code;
}

Upvotes: 1

Related Questions