4

This is my JSON from URL https://api.myjson.com/bins/142jr

[
  {
    "serviceNo":"SR0000000001",
    "serDate":"17",
    "serMonth":"DEC",
    "serYear":"2015",
    "serTime":"02.30 AM",
    "serApartmentName":"Galaxy Apartments"
  },
  {
    "serviceNo":"SR0000000002",
    "serDate":"19",
    "serMonth":"JUN",
    "serYear":"2016",
    "serTime":"03.30 AM",
    "serApartmentName":"The Great Apartments"
  }
]

I have one ListView I want populate details from online JSON,above i given a link and sample json anybody given sample jackson code in java

Thanks for advance, Rajesh Rajendiran

2 Answers 2

2

To use jackson you need to create a model class:

[
  {
    "serviceNo":"SR0000000001",
    "serDate":"17",
    "serMonth":"DEC",
    "serYear":"2015",
    "serTime":"02.30 AM",
    "serApartmentName":"Galaxy Apartments"
  },
  {
    "serviceNo":"SR0000000002",
    "serDate":"19",
    "serMonth":"JUN",
    "serYear":"2016",
    "serTime":"03.30 AM",
    "serApartmentName":"The Great Apartments"
  }
]

For the above the json the model class would be:

public class SomeClass {
 private String serviceNo;
 private String serDate;
 private String serMonth;
 private String serYear;
 private String serTime;
 private String serApartmentName;

 @JsonProperty("serviceNo") //to bind it to serviceNo attribute of the json string
 public String getServiceNo() {
  return serviceNo;
 }

 public void setServiceNo(String sNo) { //@JsonProperty need not be specified again
  serviceNo = sNo;
 }

 //create getter setters like above for all the properties.
 //if you want to avoid a key-value from getting parsed use @JsonIgnore annotation

}

Now whenever you have the above json as string stored in a variable say jsonString use the following code to parse it:

ObjectMapper mapper = new ObjectMapper(); // create once, reuse
ArrayList<SomeClass> results = mapper.readValue(jsonString,
   new TypeReference<ArrayList<ResultValue>>() { } );

results should now contain two SomeClass objects having the above json parsed as respective objects.

PS: Its been a long time since I have used Jackson for parsing so this code might need some improvements.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are getting this as http response then I would suggest to use spring rest template for android. It has support for Message Converters. That way the onus of marshaling and unmarshalling.

[Update] Here is a blog for the same :http://www.journaldev.com/2552/spring-restful-web-service-example-with-json-jackson-and-client-program

Refer Docs for more details:

http://docs.spring.io/spring-android/docs/current/reference/html/rest-template.html

1 Comment

and sample code here(ignore the spring injection part) : apieceofmycode.blogspot.in/2013/05/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.