Parsing JSON String Array of Arrays in Spring - java

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
}

Related

Need to serialize as JSON string using multiple separate keys in POJO using jackson?

I have a java class as mentioned below, In that, I want to create an inner JSON using duration & durationType while serialization using Jackson.
public class Task {
private Long id;
private String name;
private Integer duration;
private String durationType
// getters & setters...
If I serialize (POJO to JSON String) I need output as following, without creating a duration class.
{
"id" : 1001,
"name" : "Task title"
"duration" : {
"value" : 4
"type" : "hours"
}
}
If it is that possible, please let me know. Thanks in advance :)

Java Jackson Json Array Serialization

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;
}

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

Serialize a bean to json string using com.fasterxml.jackson with the following format

In general, jackson is serialize beans to standard json format, for example, the following class:
public class Person {
private String name;
private int age;
// getter/setter
}
will serialize to following json:
{
"name" : "test1111",
"age" : 18
}
but i want to get the format like this(non-standard):
{
name : test1111,
age : 18
}
that is to say, i didn't want to output the double-quote despite of the type.
Thanks for help in advance!
That is not valid JSON and therefore you cannot use Jackson to generate it.
If you want, you can use Jackson to generate a String value of
{
"name" : "test1111",
"age" : 18
}
and then manually remove the quotes.

desrialize JSON with child array using Jackson annotations?

I'm trying to parse some JSON containing a nested array. I'd like the array to map to a list of child objects within the parent I'm mapping. Here is the (slightly abbreviated) JSON and Java classes
JSON:
{
"id": "12121212121",
"title": "Test Object",
"media$content": [
{
"plfile$audioChannels": 1,
"plfile$audioSampleRate": 18000,
},
{
"plfile$audioChannels": 2,
"plfile$audioSampleRate": 48000,
},
{
"plfile$audioChannels": 2,
"plfile$audioSampleRate": 48000,
}
]
}
Java classes
class MediaObject {
#JsonProperty("id")
private String id;
#JsonProperty("title")
private String title;
#JsonProperty("media$Content")
private List<MediaContent> mediaContent;
... getters/setters ...
}
class MediaContent {
#JsonProperty("plfile$audioChannels")
private int audioChannels;
#JsonProperty("plfile$audioSampleRate")
private int audioSampleRate;
... getters/setters ...
}
I'd like to be able to deserialize using annotations along with the standard mapper code, i.e.
mapper.readValue(jsonString, MediaObject.class)
Everything works fine with the "id" and "title" fields, but my list of MediaContent objects always comes up null. This seems like something Jackson should be able to handle without much trouble, can anyone see what I'm doing wrong here?
The name of the json field is wrong - the attribute is not media$Content, rather media$[c]ontent. Otherwise I do not see why it will not work.

Categories

Resources