rrain
rrain

Reputation: 61

Accessing nested JSON values with Jackson GWT

I am currently using the jackson gwt library and I was wondering how I can access values that are nested within a json string. My current setup code looks like this:

public static interface PremiumMapper extends ObjectMapper<Premium> {}

public static class Premium {

    public float getFaceAmount() {
        return faceAmount;
    }

    public void setFaceAmount(float faceAmount) {
        this.faceAmount = faceAmount;
    }

    public float getStart() {
        return start;
    }

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

    public float getEnd() {
        return end;
    }

    public void setEnd(float end) {
        this.end = end;
    }

    public float start;

    public float end;

    public float faceAmount;
}

while my access code looks like this:

String json = "{\"errorCode\":\"validation.failed\",\"statusCode\":400,\"details\":[{\"code\":\"targetPremium.monthlyPremiumsCents.outOfBounds\",\"validChoices\":[{\"start\":9450,\"end\":125280}]}]}";

PremiumMapper mapper = GWT.create( PremiumMapper.class );

Premium premium = mapper.read( json );

System.out.println("VALUES OUTPUTTED: " + premium.getStart());

How am I able to get the start value within the json string?

Upvotes: 1

Views: 199

Answers (1)

Mohsen
Mohsen

Reputation: 4643

Try This as your Premium class:

 -----------------------------------com.example.Premium.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Premium {

private String errorCode;
private Integer statusCode;
private List<Detail> details = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public String getErrorCode() {
return errorCode;
}

public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}

public Integer getStatusCode() {
return statusCode;
}

public void setStatusCode(Integer statusCode) {
this.statusCode = statusCode;
}

public List<Detail> getDetails() {
return details;
}

public void setDetails(List<Detail> details) {
this.details = details;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Detail.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Detail {

private String code;
private List<ValidChoice> validChoices = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public List<ValidChoice> getValidChoices() {
return validChoices;
}

public void setValidChoices(List<ValidChoice> validChoices) {
this.validChoices = validChoices;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

-----------------------------------com.example.ValidChoice.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;

public class ValidChoice {

private Integer start;
private Integer end;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public Integer getStart() {
return start;
}

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

public Integer getEnd() {
return end;
}

public void setEnd(Integer end) {
this.end = end;
}

public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

And get start value like this: premium.getDetails().get(0).getValidChoices(0).getStart()

Upvotes: 2

Related Questions