{ "datetimes": [ { "date": "2016-12-14", "start_time": "08:00:00", "end_time": "22:00:25" }, { "date": "2016-12-15", "start_time": "08:00:00", "end_time": "22:00:25" }, ... ] } 

Addition to the question:

 public class UserDateDeserializer implements JsonSerializer<Date> { @Override public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { JsonObject obj = new JsonObject().getAsJsonObject("datetimes"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); JsonArray jsonArray = obj.getAsJsonArray(); jsonArray.add(new JsonPrimitive(dateFormat.format(src))); return obj; } } 

Event class that inherits from RealmObject

 private RealmList<EventDateTimes> datetimes; 

EventDateTimes class:

 public class EventDateTimes extends RealmObject { private Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } } 

PS for now I need to understand how to serialize the date field. Thank you in advance.

  • one
    This object has already been serialized. :) Explain the question, please. Are you interested in how to write code that will turn objects of such a structure into such JSON? Or how to create objects that can store this data? Or something else? - tse
  • @tse Yes, I need to write code that will turn an object into such a Json. I'm not much confused. - Satanist Devilov
  • @SatanistDevilov, you yourself answered your own question - you need to write code. How exactly you are going to do it is up to you: you can use third-party libraries (Jackson), you can write everything yourself. - AseN
  • @ 0xFFh I still did not understand how to serialize it correctly :( - Satanist Devilov
  • @ 0xFFh updated the question. I do not understand what the error is. - Satanist Devilov

3 answers 3

If there is a need to create JSON manually, this can be done using the standard JSONObject and JSONArray classes:

  try { JSONObject date1 = new JSONObject(); date1.put("date", "2016-12-14"); date1.put("start_time", "08:00:00"); date1.put("end_time", "22:00:25"); JSONObject date2 = new JSONObject(); date2.put("date", "2016-12-15"); date2.put("start_time", "08:00:00"); date2.put("end_time", "22:00:25"); JSONArray dates = new JSONArray(); dates.put(date1); dates.put(date2); JSONObject result = new JSONObject(); result.put("datetimes", dates); Log.d("happy", result.toString()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

    I historically use the Gson library for serialization. obj from the example below can be any object, the serializer will create fields in JSON according to the fields of the class being serialized. Each object in the class fields will also be serialized in the same way.

     Gson gson = new Gson(); String json = gson.toJson(obj); 

    Connects:

     compile 'com.google.code.gson:gson:2.8.0' 

    The documentation is here: https://sites.google.com/site/gson/gson-user-guide

      If the problem is not in the serializer, but in the description of the data in the classes, then you can use a special service. For example, the following is what generates http://www.jsonschema2pojo.org/ based on your example.

      Note that annotations for the serializer are placed in the classes, you can select it in the settings.

       -----------------------------------com.example.Datetime.java---------------------------------- package com.example; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class Datetime { @SerializedName("date") @Expose public String date; @SerializedName("start_time") @Expose public String startTime; @SerializedName("end_time") @Expose public String endTime; } -----------------------------------com.example.Example.java----------------------------------- package com.example; import java.util.List; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class Example { @SerializedName("datetimes") @Expose public List<Datetime> datetimes = null; }