如何将日期时间值从Json转换为Gson

时间:2015-11-30 06:51:05

标签: android json gson

我想将此json值转换为gson值。我能够获得大部分数据,唯一让我卡住的地方是“时间”。 请允许任何人帮助我。

"quantity_map": {
    "https://example.com/abc/500/": {
        "available": 4,
        "requested": 1,
        "max": 4,
        "id": "500",
        "amount": {
            "2015-12-02": 3000.0,
            "2015-12-01": 3000.0,
            "2015-11-30": 3000.0
        },
        "time": "2015-11-27 11:46:38.612147+00:00"
    },
    "https://example.com/abc/499/": {
        "available": 5,
        "requested": 1,
        "max": 5,
        "id": "499",
        "amount": {
            "2015-12-02": 5500.0,
            "2015-12-01": 5500.0,
            "2015-11-30": 5500.0
        },
        "time": "2015-11-27 11:46:38.621023+00:00"
    }
}

Json转换为gson转换代码

for (Map.Entry<String, Object> mapEntry : quantityMap.entrySet()) {
                final String key = mapEntry.getKey();
                Log.d(TAG, mapEntry.getValue().toString());
                ConversionClass value = new ConversionClass();
                try {
                    value = new GsonBuilder()
                            .setDateFormat("yyyy-mm-dd hh:mm:ss.ssssssZ")
                            .create()
                            .fromJson(mapEntry.getValue().toString(), ConversionClass.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

ConversionClass.class

public class ConversionClass{

private int available;
private int requested;
private int max;
private int id;
private HashMap<String, String> amount = new HashMap<>();
private Date time;

public void setAvailable(int available) {
    this.available = available;
}

public void setRequested(int requested) {
    this.requested = requested;
}

public void setMax(int max) {
    this.max = max;
}

public void setId(int id) {
    this.id = id;
}

public void setAmount(HashMap<String, String> amount) {
    this.amount = amount;
}

public void setTime(Date time) {
    this.time = time;
}

public int getAvailable() {
    return available;
}

public int getRequested() {
    return requested;
}

public int getMax() {
    return max;
}

public int getId() {
    return id;
}

public HashMap<String, String> getAmount() {
    return amount;
}

public Date getTime() {
    return time;
}
}

0 个答案:

没有答案