I am trying to parse json using jackson parser. Jackson parse the data successfully, but when #nil:true comes in the json, code breaks and gives an error:
Can not deserialize instance of java.lang.String out of START_OBJECT token
json string like this:
[
{
"Users": {
"UserID": "1",
"ExpirationDate": {
"#nil": "true"
}
}
}
]
but when json comes like below, then parsing happens successfully:
[
{
"Users": {
"UserID": "1",
"ExpirationDate": "2016-07-07"
}
}
}
]
Can you please let me know, how to handle such type of cases.
Related
I get below JSON string as a request body for my REST API. I don't like this JSON structure, but I don't have any control on this. It's somebody else posting this message and I have to create a REST API (POST method) and consume this message in my API. So I have to deserialize this into Java objects in my REST controller. It has list of lists objects. I tried several ways with fasterxml, but I was not successful.
{
"messages": [
[
{
"message": "message1_a",
"info": {
"timestamp": "2521013204"
}
},
{
"message": "message1_b",
"info": [
{
"message": "message1_c",
"info": {
"id": "asfa-14fs-df"
}
},
{
"message": "message1_d",
"info": {
"reason": "msg_reason",
}
}
]
}
]
]
}
Can anybody help me how my Java POJOs should look like?
It seems like a array of message.
In Java you can use Spring to transform the Json to a Object.
String url = "http://your/json/url";
ResponseEntity<Message[]> responseEntity = new RestTemplate().getForEntity(url, Message[].class);
Be sure that your entity has all the attributes of Json.
The doc: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html
Hi I am trying to parse json object below. But the problem is, inside the profile attribute, there is an attribute called fields which is sometimes a json object and sometime an json-array, so it is creating a problem when i am trying to use Gson to parse it. I followed this link but it didn't help How to dynamically handle json response array/object using Gson , so help is needed from someone who encountered this before, thanks!.
{
"users": {
"profile": [
{
"fields": {
"key": "fname",
"value": "Michael"
}
},
{
"fields": [
{
"key": "lname",
"value": "Bob"
},
{
"key": "age",
"value": "25"
}
]
}
]
}
}
I have a JSON below that I'm trying to parse to a POJO using Jackson
{
"Response": {
"userIds": [
"http://example.com:10249/User/526241869918679991"
],
"userGroupIds": [
"http://example.com:10249/UserGroup/1056659494710887089"
],
"accountIds": [
"http://example.com:10249/ServiceAccount/3354613317986071030"
],
"success": true
}
}
My Response POJO snippet is as below
private boolean success;
private List<String> accountIds;
private List<String> userIds;
private List<String> userGroupIds;
and their getter and setters
Is my declaration wrong as I'm getting "org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of java.util.List out of START_OBJECT token" error on parse"
The parse logic is as below:
jsonMapper.readValue(responseJSONString, new TypeReference<List<Response>>() {});
Any idea where I'm making the mistake?
First of all, your input starts with "Response": ... which must correspond to a field in some object. So what you have there is a json representation of a Response container:
class ResponseContainer {
Response Response;
}
Secondly, your try to parse a list but your input doesn't start with [ (which lists should start with) but { which indicates that it's an object. So if you want it to be a list, wrap the input in [ ... ]:
So either change your input to be a list:
|
V
[
{
"Response": {
"userIds": [
"http://example.com:10249/User/526241869918679991"
],
"userGroupIds": [
"http://example.com:10249/UserGroup/1056659494710887089"
],
"accountIds": [
"http://example.com:10249/ServiceAccount/3354613317986071030"
],
"success": true
}
}
]
^
|
I am using an API where I supply an input string, and it returns some keyword autocompletions and product nodes.
My goal is to deserialize the response and get a list of the autocompletion Strings I can use. I'm trying implement this in an android application with the Retrofit library, which uses gson.
First off, I'm not sure the response I have is a typical JSON response. The 'nodes' item has key / value pairs, but the input string and the autocompletions list don't seem to have keys I can use.
["pol",
["polaroid camera",
"polo",
"polo ralph lauren",
"polo ralph lauren men",
"polar heart rate monitor",
"polaroid",
"polo shirt",
"polar watch",
"police scanner",
"polar"],
[{
"nodes": [{
"alias": "electronics",
"name": "Electronics"
},
{
"alias": "electronics-tradein",
"name": "Electronics Trade-In"
}]
},
{
},
{
},
{
},
{
},
{
},
{
},
{
},
{
},
{
}],
[]]
This is my attempt at the java classes for gson to deserialize to. However, it doesn't work as from what I understand, gson needs the class variables to match the JSON keys (true for Node class but not the rest).
class Response {
String input;
List<String> keywords;
List<Node> nodes;
}
class Node {
String alias;
String name;
}
the json only has a couple of keys in it, this is largely a Json Array.
if you can change the JSON, make it more like this
{
"input" : "pol",
"keywords" : ["polaroid camera","polo",...],
"nodes": [{
"alias": "electronics",
"name": "Electronics"
},
{
"alias": "electronics-tradein",
"name": "Electronics Trade-In"
}]
}
I Have a Json which may come from other application and i need to check it whether is is in particular format. The JSON template i have is as follows,
{
"Types": {
"Type1": {
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3",
"recordList": {
"record1": [
{
"field": "value"
},
{
"field": {
"subrecord1": [
{
"subfield1": "subvalue1",
"subfield2": "subvalue2"
}
]
}
}
]
},
"date": "2010-08-21 03:05:03"
}
}
}
Is there any way to validate the JSON based on particular template or format.
You can use JSON Schema for that. JSON Schema lets you describe the format of the object graph you expect to receive, and then software implementing it lets you validate what you receive against your schema. There's an OSS Java implementation called json-schema-validator.