Answer the question
In order to leave comments, you need to log in
How to correctly set the class structure for the json object?
Good afternoon!
I can’t figure out the task:
There is a method that receives a JSON string with the following structure via OkHttp:
{
"12:30": [
{
name: "someName",
lastName: "someLastName"
},
{
name: "someName2",
lastName: "someLastName2"
}
],
"13:15": [
{
name: "someName3",
lastName: "someLastName3"
},
{
name: "someName4",
lastName: "someLastName4"
}
]
}
Answer the question
In order to leave comments, you need to log in
Here, with the help of Gson , everything is parsed well.
PS. instead of lombook.Data just create getters/setters on the inner class. It's more comfortable for me.
package com.mycompany;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Map.Entry;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class ReTest {
@lombok.Data
public static class MyData {
private String name;
private String lastName;
}
public static void main(String[] args) {
String json = "{\n" +
" \"12:30\": [\n" +
" {\n" +
" name: \"someName\",\n" +
" lastName: \"someLastName\"\n" +
" },\n" +
" {\n" +
" name: \"someName2\",\n" +
" lastName: \"someLastName2\"\n" +
" }\n" +
" ],\n" +
" \"13:15\": [\n" +
" {\n" +
" name: \"someName3\",\n" +
" lastName: \"someLastName3\"\n" +
" },\n" +
" {\n" +
" name: \"someName4\",\n" +
" lastName: \"someLastName4\"\n" +
" }\n" +
" ]\n" +
"}";
Gson parser = new Gson(); // create json parser
Type type = new TypeToken<Map<String, MyData[]>>(){}.getType(); // create custom type
Map<String, MyData[]> data = parser.fromJson(json, type); // parse data to
for( Entry<String, MyData[]> d: data.entrySet()) {
System.out.printf("%s\n", d.getKey());
for(MyData e: d.getValue()) {
System.out.printf(" %s\n", e);
}
System.out.println();
}
}
}
12:30
ReTest.MyData(name=someName, lastName=someLastName)
ReTest.MyData(name=someName2, lastName=someLastName2)
13:15
ReTest.MyData(name=someName3, lastName=someLastName3)
ReTest.MyData(name=someName4, lastName=someLastName4)
As an option, do everything with arrays, and before loading JSON, check if the object is not an array, then declare an array and load a single object into it.
If it used to be like this:
{
"12:30": {
name: "someName",
lastName: "someLastName"
}
}
{
"12:30": {
name: "someName",
lastName: "someLastName"
},
"13:15": [
{
name: "someName3",
lastName: "someLastName3"
},
{
name: "someName4",
lastName: "someLastName4"
}
]
}
, then convert everything to an array, and only then apply:{
"12:30": [
{
name: "someName",
lastName: "someLastName"
},
],
"13:15": [
{
name: "someName3",
lastName: "someLastName3"
},
{
name: "someName4",
lastName: "someLastName4"
}
]
}
If it is possible to influence the format, then you need to get rid of this, make an array.
If not, then you can write a custom json adapter for gson or moshi.
In general, I analyzed the available options and all solutions are like crutches. I think it's easier to change the structure of the resulting json to the following:
[
[
{
time: "12:30",
name: "someName",
lastName: "someLastName"
},
{
time: "12:30",
name: "someName2",
lastName: "someLastName2"
}
],
[
{
time: "13:15",
name: "someName3",
lastName: "someLastName3"
},
{
time: "13:15",
name: "someName4",
lastName: "someLastName4"
}
]
]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question