0

I have such a program that get data using API REST and show it in the JTextArea. Everything is OK but I need to modify this program with deserializing. I need to deserialized data which I get from the API to the class Country and only then show it. I tried but got the error:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of model.Country[] out of START_OBJECT token at [Source: {"nativeName":"latitude","longitude"}; line: 1, column: 1]

Here is my main class (my attempt to do deserializing is commented):

public void actionPerformed(ActionEvent evt) {

    /*ObjectMapper mapper = new ObjectMapper();
    String userDataJSON = "{\"nativeName\":\"latitude\",\"longitude\"}";
    mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

    try {
        Country[] userFromJSON = mapper.readValue(userDataJSON, Country[].class);
        System.out.println(userFromJSON);
    } catch (IOException e1) {
        System.out.println(e1);
    }*/
            ...
                textField.selectAll();
                textArea.setCaretPosition(textArea.getDocument().getLength());
            }
        } catch (Exception e) {
        }
    }
}

And my class Country:

public class Country {

@JsonProperty
private String name; 
@JsonProperty
public String nativeName;
@JsonProperty
private String latitude; 
@JsonProperty
private String longitude; 
@JsonProperty
private String currencyCode; 

public Country(String name, String nativeName, String latitude, String longitude, String currencyCode) {
    super();
    this.name = name;
    this.nativeName = nativeName;
    this.latitude = latitude;
    this.longitude = longitude;
    this.currencyCode = currencyCode;

// getters & setters
}
Viola
  • 487
  • 1
  • 10
  • 33
  • You're trying to deserialize **one** country object as JSON it to an **array** of countries. – JB Nizet Dec 02 '17 at 13:54
  • so what I should to do? – Viola Dec 02 '17 at 14:00
  • You want **one** Country, so you should tell Jackson that you want that as the result of the deserialization: `Country country = mapper.readValue(userDataJSON, Country.class);` – JB Nizet Dec 02 '17 at 15:25

1 Answers1

-1

Make 'userDataJSON' as given below

String userDataJSON = "[{\"name\":\"test\", \"nativeName\":\"Test\", \"latitude\":\"45.898\",\"longitude\":\"76.8989\", \"currencyCode\":\"code\"}]";

and remove the constructor from the Country class.

anil
  • 1
  • 1
  • I believe the asker is trying to read from an API not a static string since `userDataJSON` is currently commented out in the code. I don't think changing it is the solution. Also, why should they remove the constructor from the country class? Be sure to explain why your answer would work. – Loren Dec 02 '17 at 15:37
  • I didnt consider 'userDataJSON ' as static one. but whatever given in the question is not a valid JSON, for the second question [https://stackoverflow.com/questions/39123030/jackson-json-deserialization-with-multiple-parameters-constructor] – anil Dec 02 '17 at 15:53