Java Jackson Json Array Serialization - java

I have a special case that I have been trying to serialize with Jackson. Here is a sample.
{
"key1": [
[
10,
11
],
[
12,
13
]
],
"key2": [
[
14,
15
],
[
16,
17
]
]
}
How can I make a model with decorators that will serialize this payload?
EDIT:
Assume there is a JSON file with the above contents. How can I make a POJO that can be used to serialize the above JSON like so with Jackson:
MyPOJO pojo = objectMapper.readValue(new File("above_json.json"), MyPOJO.class);
Here is a very simple example of what I am looking for:
JSON Input:
"Parameter":{
"Name":"Parameter-Name",
"Value":"Parameter-Value"
}
POJO to serialize above simple JSON:
public class Parameter {
#JsonProperty("Name")
public String name;
#JsonProperty("Value")
public String value;
}
I am looking for a class, just like Parameter above that will serialize the example JSON that I provided originally above, rather than the simplified JSON.

Assume there is a JSON file with the above contents. How can I make a POJO that can be used to serialize the above JSON like so with Jackson
This is very simple, you need to understand the data of your JSON. You have key-value and each value is Array of another Array. That is similar to
class POJO
{
List<List<Integer>> key1;
List<List<Integer>> key2;
}

Related

Parsing JSON String Array of Arrays in Spring

I am trying to parse json string in spring using pojo classes. I've a json string as:
{
"values": [
[
1509836400000,
670042.375,
2
],
[
1509836400000,
670042.375,
2
]
]
}
Can somebody help me how do I parse this string to object of the form
class Value{
long timeStamp;
double value;
int flag;
// getters and setters
}
class Values{
Value[] values;
// getters and setters
}
use Gson library to parse Json to Java like this.
Gson gson = new Gson();
Values values = gson.fromJson(yourJsonString , Values.class);
you can convert Json to Java POJOs using this tool Json to Java
You can use jackson 2
Reference
you can use the jackson library which is able of converting the string object to the desired pojo class
so please check this example
https://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
If you are able the change the json to meet your model/pojo
just convert it from array to object like this Json:
{
"values": [ {
"timeStamp" : 1509836400000,
"value" : 670042.375,
"flag" : 2
},
{
"timeStamp" : 1509836400000,
"value" : 670042.375,
"flag" : 2
} ]
}
If you aren't able to change the json, you can map the values as List<List<Double>>, and then get every one by index: 0 for timeStamp and so on. Your model/pojo should be:
class Values {
List<List<Double>> values;
// getters and setters
}

JSON assign Java String

My json structure is
{
"GAME_CUSTOMIZE": {
"GAME_CODE": "MOCK12V2.0",
"RESULT": {
"response": [
{
"id": "PLAYER1",
"value": "WERYT"
},
{
"id": "PLAYER2",
"value": "QEWRET"
},
{
"id": "PLYAER3",
"value": "765422"
}
]
}
}
}'
My Pojo object using for mapping is,
import com.fasterxml.jackson.annotation.JsonProperty;
#JsonProperty("GAME_CODE")
private String gameCode;
#JsonProperty("RESULT")
private String responseJson;
When trying to assign the "RESULT" complete json to string, getting empty after conversion from json to Java object. Have tried JsonRawValue annotation also. And tried with responseJson as Object type instead of string. I want to get the RESULT Json to Java string or Object property.
The default mapping for a JSON object is a LinkedHashMap<String, Object>. The simple solution would be to change the type of responseJson to that.
Mapping to a String (containing the original JSON serialization of the object) might make sense, but I don't know if it is implementable. Mapping to java.lang.Object makes no sense.
UPDATE - the following Q&A explains how to deserialize an object as a string:
Jackson Deserialize Variable as Json String

Gson Deserialization json which contains parameter that can be string or object

I have a Json like below.
{
"myItemArray": [
{
"id": "c8c1",
"price": 18,
"display": {
"inneritemName1": "innerItemValue1",
"inneritemName2": "innerItemValue2",
"inneritemName3": "innerItemValue3"
}
},
{
"id": "cac1",
"price": 2,
"display": "Lemonate"
}
]
}
As you can see that the item in my array has a parameter called "display" which can be String or Object. How can I deserialize this json using Gson?
I don't want to deserialize this string manually is there any other way to to this?
You can use Gson, but have to build a class, get each Json attribute into the relevent attribute in the Java class.
I highly recomand Jackson library, you only need to setup the Java class, and it takes care of the mapping and parsing.

GSON How to deserialize to a class with this array json string

I am struggling to find a way to serialize / deserialize this JSON output to a Java class? Can anyone provide code sample?
[
{
"topic": "this is my topic"
},
[
{
"name": "John"
},
{
"age": 100
}
]
]
My current attempt uses this Javabean:
public class Test {
private String topic;
private List<Person> listOfPersons;
}
Which I try to deserialize data into using Gson:
gson.fromJson(this.json, Test[].class);
But the deserialization fails, because Gson is looking for a list of persons in the JSON, but it doesn't exist.
It doesn't seem like having an object next to an array, inside an array, is sensical. It would make sense to put things this way:
{
"topic": "this is my topic",
"listOfPersons" : [
{
"name": "John",
"age": 100
},
{
... another person
}
]
}
Then you could just have a Person class:
public class Person {
private String name;
private int age;
}
...and you could deserialize with the code you already have.
The problem here is that your JSON data is syntactically correct, but semantically ambiguous. And by that I mean, it appears to represent a polymorphic array, where each item in the array is of a different type.
In addition, the portion representing a 'person' seems to have been de-normalized horribly, where each attribute of a person is represented as a separate object in an array. Quite weird indeed. Unfortunately its really impossible to tell what types are represented just by looking at the data alone, and there are no hints provided to allow Gson to deserialize the data for you. The best you can do in this case is manually parse the information.
Test test = new Test();
JsonArray rootArray = new JsonParser().parse(jsonString);
test.setTopic(rootArray.get(0).get("topic");
Person person = new Person();
JsonArray personArray = rootArray.get(1);
person.setName(personArray.get(0).get("name"));
person.setAge(personArray.get(1).get("age"));
test.setListOfPersons(Arrays.asList(person));

Deserializing multi-dimensional array GSON

I want to deserialize the following json data https://mtgox.com/code/data/getDepth.php. I keep getting an error. Below is my code.
Gson gson = new Gson();
String json = readHTTPS(new URL("https://mtgox.com/code/data/getDepth.php"));
AskBids askBids = gson.fromJson(json, AskBids.class);
My AskBids class look like:
public class AskBids {
private String [] [] asks;
private String [] [] bids;
public AskBids(){}
}
The error is get is com.google.gson.JsonParseException: Expecting object found: "asks"
Any ideas? Thanks
The JSON linked in the original question contains over 2,800 JSON tokens, element names and values. Here is a small section of that example that maintains the exact same structure.
{
"asks": [
[
18.22,
15.362
],
[
25.4682,
20
]
],
"bids": [
[
18.06,
50
],
[
18.0099,
32.64
]
]
}
That said, I copy-pasted the deserialization code from the original question, using both the original JSON and the shorter version I pasted above, and the code ran without error as expected.
Based on the error message Expecting object found: "asks", I suspect that the readHTTPS(URL) method is not returning the correct result, in that it is not including the opening { of the JSON. If this is the problem, but for some reason readHTTPS(URL) cannot be fixed, you could always "fix" its output by just concatenating the missing character(s).

Categories

Resources