Android GSON parses the data of List with Date in the background and how the background interacts

Keywords: Java JSON Google network

The code returned by interacting with the background is 200. It's important to analyze the List data of JSON. I use SSM in the background. Its JSON is very friendly. In addition to the Date type, first paste the code learned by novices. Many customized online transformations don't need to be so complicated. One line of code completes the Date transformation

//Attach the bag, the big guy jumps over, Xiaobai doesn't understand the message
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

//Now define ArrayList < > ()
 private List<RunreleaseCustom> runreleaseCustomList = new ArrayList<>();


//The following is written under the function of network interaction return

 Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
//Parsing List 
 runreleaseCustomList = gson.fromJson(json, new TypeToken<List<RunreleaseCustom>>() {
                }.getType());

 

The annotation @ RequestBody of the background SSM framework does not support the transformation of Date type. Maybe the new version supports it! If you don't support it, you can see that. You need to serialize and deserialize Date

 

DateJsonSerializer .java

 

package JsonTest;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;

public class DateJsonSerializer extends JsonSerializer<Date> {  
	  
    /** 
     * @see com.fasterxml.jackson.databind.JsonSerializer#serialize(java.lang.Object, 
     *      com.fasterxml.jackson.core.JsonGenerator, 
     *      com.fasterxml.jackson.databind.SerializerProvider) 
     */  
    @Override  
    public void serialize(Date date, JsonGenerator generator, SerializerProvider provider)  
            throws IOException, JsonProcessingException {  
        ObjectMapper om = new ObjectMapper();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        om.writeValue(generator, sdf.format(date));  
    }  
  
}

 

 DateJsonDeserializer.java

package JsonTest;

import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;



public class DateJsonDeserializer extends JsonDeserializer<Date> {  
	  
    /** 
     * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, 
     *      com.fasterxml.jackson.databind.DeserializationContext) 
     */  
    @Override  
    public Date deserialize(JsonParser parser, DeserializationContext context)  
            throws IOException, JsonProcessingException {  
        try {  
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
            return sdf.parse(parser.getValueAsString());  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
  
} 

Then just add a comment to the class with Date field in your POJO, as follows

 

public class Runrelease {
    private Integer releaseid;

    private String releasePublisherid;

    @JsonDeserialize(using = DateJsonDeserializer.class) 
    @JsonSerialize(using = DateJsonSerializer.class)  
    private Date releasePicktime;
}

 

Posted by freynolds on Sun, 05 Jan 2020 15:08:12 -0800