E
E
Evgeny Petrov2019-08-12 08:39:07
Java
Evgeny Petrov, 2019-08-12 08:39:07

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"
      }
   ]
}

The problem is that an object can have one time with its objects or several as in the example above, and plus what time it will be is not known, i.e. dynamic quantity. Before that, there was statics, I solved it simply by declaring a class with the necessary fields in accordance with the json object, but I can’t understand the dynamics.
I ask for help experienced. Thank you in advance

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Cheremisin, 2019-08-12
@kazsat

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

And the conclusion.
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)

Z
Zakharov Alexander, 2019-08-12
@AlexZaharow

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

And now and so and so:
{
   "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"
      }
   ]
}

D
Denis Zagaevsky, 2019-08-12
@zagayevskiy

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.

E
Evgeny Petrov, 2019-08-12
@kazsat

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"
      }
   ]
]

With such a structure, it is easy to describe a class for json

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question