Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Gson在给定类上映射时,是否可以修改已解析的数据_Java_Gson - Fatal编程技术网

Java Gson在给定类上映射时,是否可以修改已解析的数据

Java Gson在给定类上映射时,是否可以修改已解析的数据,java,gson,Java,Gson,我有以下JsonArray文件 [ { "DateSent": "06/22/2014 11:09:11", "UserName": "santosh" } ] 我将它映射到下面的AssistResponse类 public class AssistResponse { @SerializedName("DateSent") private String dateSent; @SerializedName("UserName") public String user

我有以下
JsonArray
文件

 [
  {
    "DateSent": "06/22/2014 11:09:11",
    "UserName": "santosh"
  }
]
我将它映射到下面的
AssistResponse

public class AssistResponse {

@SerializedName("DateSent")
private String dateSent;
@SerializedName("UserName")
public String userName;

public void setDateSent(String dateSent) {
    this.dateSent = dateSent;
    System.out.println(this.dateSent);
}

public String getDateSent() {
    return this.dateSent;
}

}
我可以很容易地得到这个类的
列表或数组
。但是我确实想修改class
AssistResponse
dateSent
属性,而
Gson
映射这个类。意思是
Gson
,解析
Json
文件,然后将值分配给
@SerializedName
。因此,在分配值时,我需要更改
dateSent
值。
dateSent
包含
服务器日期
值,但我想将其更改为
本地时间
。我可以使用
parsed列表
,然后对其进行迭代,获得
dateSent
值,然后对其进行更改。但是,在通过
Gson
解析
Json
的过程中,是否可以更改该值,以便在最后我不必再次迭代整个
数组

谢谢

dateSent是否需要是字符串?你为什么不能把它作为日期呢

我想你可以为此编写一个gson反序列化程序。 您可以在反序列化程序中修改日期并返回修改后的日期

JsonDeserializer<Date> deser = new JsonDeserializer<Date>() {
  @Override
  public Date deserialize(JsonElement json, Type typeOfT,
       JsonDeserializationContext context) throws JsonParseException {
           Date newDate = //TODO: modifications;
           return newDate;

  }
};
JsonDeserializer desr=新的JsonDeserializer(){
@凌驾
公共日期反序列化(JsonElement json,类型typeOfT,
JsonDeserializationContext)抛出JsonParseException{
Date newDate=//TODO:修改;
返回newDate;
}
};
您需要根据需要将日期从JSON字符串更改为本地日期,并在自定义POJO类中设置值,如下所示

有关更多信息,请查看

我已经在同样的背景下发布了它,这可能有助于你把它说得更清楚


您可以使用
Date
作为
dateSent
的数据类型,如果不想更改,也可以保持原样

示例代码:

class AssistResponse {

    @SerializedName("DateSent")
    private Date dateSent;
    @SerializedName("UserName")
    public String userName;
    // getter & setter
}


class AssistResponseDeserializer implements JsonDeserializer<AssistResponse> {

    @Override
    public AssistResponse deserialize(final JsonElement json, final Type typeOfT,
            final JsonDeserializationContext context) throws JsonParseException {

        JsonArray jsonArray = json.getAsJsonArray();
        JsonElement jsonElement = jsonArray.get(0);
        JsonObject jsonObject = (JsonObject) jsonElement;

        AssistResponse assistResponse = new AssistResponse();
        assistResponse.setUserName(jsonObject.get("UserName").getAsString());

        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String dateString = jsonObject.get("DateSent").getAsString();
        try {
            Date date = dateFormat.parse(dateString);
            Date localDate=...              // <== here change it to local date
            assistResponse.setDateSent(localDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return assistResponse;
    }
}
AssistResponse data = new GsonBuilder()
        .registerTypeAdapter(AssistResponse.class, new AssistResponseDeserializer())
        .create().fromJson(jsonString, AssistResponse.class);

System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));
class AssistResponse{
@SerializedName(“DateSent”)
已发送的私人日期;
@序列化名称(“用户名”)
公共字符串用户名;
//吸气剂和塞特
}
类助理响应的序列化程序实现JsonDeserializer{
@凌驾
公共助理响应反序列化(最终JsonElement json,最终类型typeOfT,
最终JsonDeserializationContext)抛出JsonParseException{
JsonArray JsonArray=json.getAsJsonArray();
JsonElement=jsonArray.get(0);
JsonObject JsonObject=(JsonObject)jsonElement;
AssistResponse AssistResponse=新的AssistResponse();
assistResponse.setUserName(jsonObject.get(“用户名”).getAssString());
SimpleDataFormat dateFormat=新的SimpleDataFormat(“MM/dd/yyyy HH:MM:ss”);
String dateString=jsonObject.get(“DateSent”).getAsString();
试一试{
Date=dateFormat.parse(日期字符串);

Date localDate=…//感谢您提供的解决方案,并为我指明了正确的方向。
class AssistResponse {

    @SerializedName("DateSent")
    private Date dateSent;
    @SerializedName("UserName")
    public String userName;
    // getter & setter
}


class AssistResponseDeserializer implements JsonDeserializer<AssistResponse> {

    @Override
    public AssistResponse deserialize(final JsonElement json, final Type typeOfT,
            final JsonDeserializationContext context) throws JsonParseException {

        JsonArray jsonArray = json.getAsJsonArray();
        JsonElement jsonElement = jsonArray.get(0);
        JsonObject jsonObject = (JsonObject) jsonElement;

        AssistResponse assistResponse = new AssistResponse();
        assistResponse.setUserName(jsonObject.get("UserName").getAsString());

        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String dateString = jsonObject.get("DateSent").getAsString();
        try {
            Date date = dateFormat.parse(dateString);
            Date localDate=...              // <== here change it to local date
            assistResponse.setDateSent(localDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return assistResponse;
    }
}
AssistResponse data = new GsonBuilder()
        .registerTypeAdapter(AssistResponse.class, new AssistResponseDeserializer())
        .create().fromJson(jsonString, AssistResponse.class);

System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(data));