I have a task to parse Json to Java Class.
Json fragment I am trying to parse and have problems with is something like tree structure.
The key point here is that parameterValue
may be string, or another array of parameter name/value pairs;
I would like to use Jackson mapper
Response response = mapper.readValue(jsonObjstring, Response.class);
The problem here is that I do not know how can I describe Response class here, so it could be auto parsed by Jackson. If it is even possible.
Json:
[{
"parameterName" : "name",
"parameterValue" : "value"
}, {
"parameterName" : "apnRecord",
"parameterValue" : [{
"parameterName" : "name",
"parameterValue" : "value"
}, {
"parameterName" : "name",
"parameterValue" : "value"
}, {
"parameterName" : "name",
"parameterValue" : "value"
}, {
"parameterName" : "name",
"parameterValue" : "value"
}, {
"parameterName" : "name",
"parameterValue" : "value"
}, {
"parameterName" : "name",
"parameterValue" : "value"
}]
}]
You can try this online json to java pojo mapper, It produces the required result and it comes in handy for me many times.
http://pojo.sodhanalibrary.com/
Try it and if you face any more difficulties provide the complete json you receive, we can work out some thing else.
Create a POJO Response.java class which contains your fields, and getters/setters, create a standard constructor and one which contains your fields, and a toString method(). I show you an example with Annotations
define class like:
@JsonAutoDetect
@JsonSerialize
public class Response implements Serializable
then your fields with
@JsonProperty
private String parameterName;
and lastly your constructors fe like this
@JsonCreator
public Response(@JsonProperty("parameterName") String parameterName) {
this.parameterName = parameterName;
}
I have defined it as
private List<List<Object>> resultParameters;
then jackson maper parses it (Object) to LinkedHashMapEnty when it is final value or to ArrayList - if it is not a value but another List of values;
It can be closed;
package test;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public static void main(String[] args) {
String readJsonAsString = "[{\r\n \"parameterName\" : \"name\",\r\n \"parameterValue\" : \"value\"\r\n}, {\r\n \"parameterName\" : \"apnRecord\",\r\n \"parameterValue\" : [{\r\n \"parameterName\" : \"name\",\r\n \"parameterValue\" : \"value\"\r\n }, {\r\n \"parameterName\" : \"name\",\r\n \"parameterValue\" : \"value\"\r\n }, {\r\n \"parameterName\" : \"name\",\r\n \"parameterValue\" : \"value\"\r\n }, {\r\n \"parameterName\" : \"name\",\r\n \"parameterValue\" : \"value\"\r\n }, {\r\n \"parameterName\" : \"name\",\r\n \"parameterValue\" : \"value\"\r\n }, {\r\n \"parameterName\" : \"name\",\r\n \"parameterValue\" : \"value\"\r\n }]\r\n}]";
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNodes = mapper.readTree(readJsonAsString.toString());
for (JsonNode jsonNode : jsonNodes) {
System.out.println(jsonNode.get("parameterName"));
}
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1st need to convert json to string format, then by ObjectMapper need to convert it into tree structure.
for this you need to use 3 jars : jackson-annotations-2.3.5.jar jackson-core-2.3.5.jar jackson-databind-2.3.5.jar
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.