Mike Rotch
Mike Rotch

Reputation: 19

How to consume REST in Spring/Java with JSON unnamed values?

I am new to JSON and REST. I am using Spring's RestTemplate to consume JSON. I was able to do it with this JSON response with this API call: http://data.fcc.gov/api/block/find?latitude=39.9936&longitude=-105.0892&showall=false&format=json

JSON Response:

    {"Block":{"FIPS":"080130608005010"},"County":
    {"FIPS":"08013","name":"Boulder"},"State":
    {"FIPS":"08","code":"CO","name":"Colorado"},"status":"OK","executionTime":"8"}

However, when I take this FIPS code from this request and try to use it to request information from the census with this call: http://api.census.gov/data/2012/acs5?get=B19001_002E&for=tract:060800&in=state:08+county:013&key=

This is the JSON Response I get:

    [["B19001_002E","state","county","tract"],
    ["225","08","013","060800"]]

As you can see all of the 'variables' are unnamed and in arrays. I am not sure how to consume this using Spring's RestTemplate using a POJO.

This is the Java code I have (where URL is the string of the API Call):

RestTemplate restTemplate = new RestTemplate();
CensusData cd = restTemplate.getForObject(URL, CensusData.class);
System.out.println("data: " + cd.getData());

Here is my POJO (CensusData):

@JsonIgnoreProperties(ignoreUnknown = true)
public class CensusData {

     @JsonProperty
    private List<List<String>> data;

    public String getData() {
        String str = "";
        for(List<String> list : data) {
            for(String s : list) {
                str += s;
                str += " ";
            }
        }
        return str;
    }
}

The problem is that I don't know what to name "data" in my CensusData object, since the values are unnamed in the JSON response. So I get this exception thrown:

Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of template.main.CensusData out of START_ARRAY token

Upvotes: 1

Views: 2081

Answers (3)

Lei Niu
Lei Niu

Reputation: 51

Using Gson https://code.google.com/p/google-gson/ .

String result = getResponseFromServer(String url); //restful
Gson gson = new Gson();
String[][] str = gson.fromJson(result, String[][].class);

Upvotes: 2

Edward J Beckett
Edward J Beckett

Reputation: 5140

The real issue is that you haven't expanded the variables to create the mappings. You need to create the object mappings for the Census Data API: Variables. Further introspection of the request parameter for B19001_002E, expands to the definition and corresponding json for Less than 10,000. Which contains the following definition.

{
    "name": "B19001_002E",
    "label": "Less than $10,000",
    "concept": "B19001.  Household Income",
    "predicateType": "int"
}

Once you've successfully created this POJO, you can then reference the responses and move on to the next challenge.

Upvotes: 0

Dino Tw
Dino Tw

Reputation: 3321

The response from census is value array, there is no key-value object, you don't use POJO for the mapping. The ObjectMapper#readValue() from Jackson may help you make the parsing task easier. Look the section Tree Model Example in JacksonInFiveMinutes for examples.

Upvotes: -1

Related Questions