{
"12:30": [
{
name: "someName",
lastName: "someLastName"
},
{
name: "someName2",
lastName: "someLastName2"
}
],
"13:15": [
{
name: "someName3",
lastName: "someLastName3"
},
{
name: "someName4",
lastName: "someLastName4"
}
]
}
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)
{
"12:30": {
name: "someName",
lastName: "someLastName"
}
}
{
"12:30": {
name: "someName",
lastName: "someLastName"
},
"13:15": [
{
name: "someName3",
lastName: "someLastName3"
},
{
name: "someName4",
lastName: "someLastName4"
}
]
}
, то конвертите всё в массив, а только потом применяйте:{
"12:30": [
{
name: "someName",
lastName: "someLastName"
},
],
"13:15": [
{
name: "someName3",
lastName: "someLastName3"
},
{
name: "someName4",
lastName: "someLastName4"
}
]
}
[
[
{
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"
}
]
]