在json中的Gson中设置日期格式 [英] set date format in Gson in jsp

查看:106
本文介绍了在json中的Gson中设置日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Gson将Json转换为Java中的对象。我试图转换的Json具有相当复杂的结构。

  {
name:0,
dtExpiration:07/14/2011 00:00,
quotaList:null,
question_array:[
{
question :0,
questionType:resposta de texto,
min:null,
max:null,
responceList :[],
answer_array:[
{
...

1.问题是:将gson.fromJson(json,SuperClass.class)工作,因为这个类有ArrayLists?



我看不到它是否有效,因为我的日期格式有问题。程序抛出异常:

 导致:java.text.ParseException:Unparseable date:2011/7/15 00:00 

所以我试着用下面的方式指定格式:

  GsonBuilder gson = new GsonBuilder()。setDateFormat(mm / dd / yyyy hh:mm); 

但结果相同。

2.你可以向我解释如何使用GsonBuilder更改日期格式吗?



谢谢

解决方案


1.问题是:将gson.fromJson(json,SuperClass.class)工作,因为这个类有ArrayLists?


是的,只要 SuperClass 结构与JSON结构相匹配


2.您可以向我解释如何使用GsonBuilder更改日期格式吗?


使用大M同月。小的m表示每小时的分钟数。



以下是一个工作示例。

  import java.io.FileReader; 
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Foo
{
public static void main(String [] args)throws Exception
{
Gson gson = new GsonBuilder()。setDateFormat (MM / dd / yyyy hh:mm)。create();

SuperClass sc = gson.fromJson(new FileReader(input.json),SuperClass.class);
System.out.println(gson.toJson(sc));
// {name:0,dtExpiration:07/14/2011 12:00,question_array:[{question:0,questionType:resposta de texto , 分钟: 空, 最大值: 空, responceList:[], answer_array:[{ ID:1, 回答: 是},{ ID :2,answer:no}]}]}
}
}

class SuperClass
{
String name;
日期dtExpiration;
列表< String> quotaList;
问题[] question_array;
}

class问题
{
int问题;
字符串questionType;
String min;
String max;
列表< String> responceList;
Answer [] answer_array;
}

class回答
{
int id;
字符串答案;
}

请注意,在我的系统中,它从24H时钟显示更改为12H时钟显示。

I'm using Gson to convert Json to an object in Java. Json that i'm trying to convert has quite complicated structure.

{
   "name": "0",
   "dtExpiration": "07/14/2011 00:00",
   "quotaList": null,
   "question_array": [
      {
         "question": "0",
         "questionType": "resposta de texto",
         "min": "null",
         "max": "null",
         "responceList": [],
         "answer_array": [
            {
...

1.The question is: will gson.fromJson(json, SuperClass.class) work, as this class has ArrayLists?

I can't see if it works, because I have a problem with date format. The program throws exception:

Caused by: java.text.ParseException: Unparseable date: "07/15/2011 00:00"

So I had tried to specify format by using:

GsonBuilder gson = new GsonBuilder().setDateFormat("mm/dd/yyyy hh:mm");

but result is the same.

2.Can you explain me how to change date format using GsonBuilder?

Thank you

解决方案

1.The question is: will gson.fromJson(json, SuperClass.class) work, as this class has ArrayLists?

Yes, provided the SuperClass structure matches the JSON structure.

2.Can you explain me how to change date format using GsonBuilder?

Use big "M" for month in year. Small "m" is for minute in hour.

Here's a working example.

import java.io.FileReader;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new GsonBuilder().setDateFormat("MM/dd/yyyy hh:mm").create();

    SuperClass sc = gson.fromJson(new FileReader("input.json"), SuperClass.class);
    System.out.println(gson.toJson(sc));
    // {"name":"0","dtExpiration":"07/14/2011 12:00","question_array":[{"question":0,"questionType":"resposta de texto","min":"null","max":"null","responceList":[],"answer_array":[{"id":1,"answer":"yes"},{"id":2,"answer":"no"}]}]}
  }
}

class SuperClass
{
  String name;
  Date dtExpiration;
  List<String> quotaList;
  Question[] question_array;
}

class Question
{
  int question;
  String questionType;
  String min;
  String max;
  List<String> responceList;
  Answer[] answer_array;
}

class Answer
{
  int id;
  String answer;
}

Note that on my system, it changed from the 24H clock display to the 12H clock display.

这篇关于在json中的Gson中设置日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆