Data from JSON in Android



  • I'm doing a little test app on Android that would get the WCF service data, read them and put them on the screen. I made a WCF that would transform the elements of the list(List) into a json line of this kind:

    "[{\"LastName\":\"Baggins\",\"FirstName\":\"Frodo\",\"Street\":\"Bagshot Row\"}, 
    {\"LastName\":\"Kurata\",\"FirstName\":\"Deborah\",\"Street\":\"Main Street\"}]"
    

    And I have a question - how does this line in the Android clint have the right to swallow, and they're gonna set up a list similar to that of c# for use, let's say, "ListView? Java is a very bad friend, and I don't know most of his functions.



  • Add Jackson.

    It's as far as we want. If the objects in the mass are always the same, it is better to create a class like this:

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Person {
        @JsonProperty("LastName")
        public String mLastName;
    
    @JsonProperty("FirstName")
    public String mFirstName;
    
    @JsonProperty("Street")
    public String mStreet;
    

    }

    Spit it out:

    String jsonString = // здесь ваша строка с данными
    ObjectMapper mapper = new ObjectMapper();
    List<Person> list = mapper.readValue(jsonString,
    new TypeReference<ArrayList<Person>>() {});

    Now mapper You have a list of objects.




Suggested Topics

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