I am trying to convert a kafka message which is received as List of string into Json object.
Following is my code snippet:
//Kafka consuming msg
List<String> message = KafkaMessageConsumer.consumeMessage(props.getProperty("pickbegin.topicname"));
System.out.println("Message:" +message);
I am getting the message from the kafka as:
Message:[{
"orders": [
{
"orderId": "784",
"orderPriority": "REGULAR",
"userId": "123"
}
]
Now I am trying to covert into Java object:
My Pojo classes are as follows:
@Data
public class PickBeginJson {
@JsonProperty("orders")
private List<Order> mOrders;
}
And another POJO class is:
@Data
public class Order {
@JsonProperty("orderId")
private String mOrderId;
@JsonProperty("orderPriority")
private String mOrderPriority;
@JsonProperty("userId")
private String mUserId;
}
My code snippet where I am trying to covert into Java object is as follows:
//Json to java object
Gson gson = new Gson();
for(String i : message)
{
JsonReader reader = new JsonReader(new StringReader(i));
reader.setLenient(true);
pickBeginJson = gson.fromJson(i, PickBeginJson.class);
System.out.println("pickBeginJson"+pickBeginJson);
}
I am getting an exception:
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 6 column 4 path $.orders[0].
I have also used ObjectMapper as:
for(String i : message)
{
pickBeginJson = objectMapper.readValue(i, PickBeginJson.class);
System.out.println("pickBeginJson"+pickBeginJson);
}
But I am getting an exception as:
com.fasterxml.jackson.databind.JsonMappingException: Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
at [Source: (String)"{
"orders": [
{
"orderId": "784",
"orderPriority": "REGULAR"
"userId": "123"
}
]
}
Any help will be appreciated.
I have seen your problem, it is a malformed JSON as the error message states. Look at your JSON in the Kafka message:
{
"orders": [
{
"orderId": "784",
"orderPriority": "REGULAR"
"userId": "123"
}
]
If you take a closer look will notice it is missing a comma between the "orderId" and "orderPriority". Also, it is missing a close bracket "}" in the end of the JSON.
Your JSON should be like this:
{
"orders":[
{
"orderId":"784",
"orderPriority":"REGULAR",
"userId":"123"
}
]
}
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.