Rest Api json example

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;


public class MainClass {

public static void main(String[] args) {
JsonParser();
}
public static void JsonParser() {
try {
ObjectMapper om = null;
JsonParser jp = null;
RootObject result = new RootObject();

jp = new JsonFactory().createParser(new URL(URL));
om = new ObjectMapper();
result = om.readValue(jp,RootObject.class);
Response response = result.getResponse();

System.out.println(response.getNumFound());
System.out.println(response.getStart());

for (Doc value : response.getDocs()) {
System.out.println( value.getId());
}

} catch (JsonParseException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}




public class Doc
{
@jsonproperty  private String id;

  public String getId() { return this.id; }

  public void setId(String id) { this.id = id; }
}

public class Response
{
  @jsonproperty("numFound") private int numFound;

  public int getNumFound() { return this.numFound; }

  public void setNumFound(int numFound) { this.numFound = numFound; }

 @jsonproperty("start") private int start;

  public int getStart() { return this.start; }

  public void setStart(int start) { this.start = start; }

  @jsonproperty("docs") private ArrayList<Doc> docs;

  public ArrayList<Doc> getDocs() { return this.docs; }

  public void setDocs(ArrayList<Doc> docs) { this.docs = docs; }
}

public class RootObject
{
  @jsonproperty("response") private Response response;

  public Response getResponse() { return this.response; }

  public void setResponse(Response response) { this.response = response; }
}

Yorumlar

Bu blogdaki popüler yayınlar

Bir Yazılımcının karşılaşabileceği Mülakat Soruları Karşılaştıklarım

[Java] Append Nedir

WebSocket Nedir?