从JSON文件中读取日期数值到Java HashMap中

huangapple go评论72阅读模式
英文:

Read date values from a JSON file into Java HashMap

问题

这是一个JSON文件,其结构如下:

{
    "calendar": {
        "dateToDayId": {
            "2016-07-14": 290356,
            "2016-08-26": 380486,
            "2016-09-07": 417244,
            "2016-08-15": 354271,
            "2016-07-25": 311762
        },
        "dishIdToMealId": {
            "1228578": 474602,
            "1228585": 474602,
            "1228586": 474602
            // 更多字段
        }
    }
}

我正在尝试将dateToDayId下的<date, number>键值对读入到Java中的HashMap<Date, Long>中。首先我创建了一个名为JsonInfo的类,其中包含一个Calendar类对象。Calendar类对象依次包含了DateToDayIdDishToMealId等所有类。我的DateToDayId类如下(我使用Jackson来解析JSON文件):

package jsonfields;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import deserializers.CustomDateDeserializer;

import java.util.Date;
import java.util.TreeMap;

public class DateToDayId {
    @JsonDeserialize(using = CustomDateDeserializer.class)
    private TreeMap<Date, Long> dateToDayMappings;

    public TreeMap<Date, Long> getDateToDayMappings() {
        return dateToDayMappings;
    }

    public void setDateToDayMappings(TreeMap<Date, Long> dateToDayMappings) {
        this.dateToDayMappings = dateToDayMappings;
    }

    @Override
    public String toString() {
        return "DateToDayId{" +
                "dateToDayMappings=" + dateToDayMappings +
                '}';
    }
}

我的Main类如下:

import java.io.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("path\\to\\file\\test.json"));
            JsonInfo jsonInfo = objectMapper.readValue(inputStreamReader, JsonInfo.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

当我运行时,出现了以下异常:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "2016-07-14" (class jsonfields.DateToDayId), not marked as ignorable (one known property: "dateToDayMappings"])
 at [Source: (InputStreamReader); line: 1, column: 48] (through reference chain: JsonInfo["calendar"]->jsonfields.Calendar["dateToDayId"]->jsonfields.DateToDayId["2016-07-14"])

我该如何修复这个问题?

英文:

I have a JSON file that looks like this:

{
    &quot;calendar&quot;: {
        &quot;dateToDayId&quot;: {
            &quot;2016-07-14&quot;: 290356,
            &quot;2016-08-26&quot;: 380486,
            &quot;2016-09-07&quot;: 417244,
            &quot;2016-08-15&quot;: 354271,
            &quot;2016-07-25&quot;: 311762
        },
        &quot;dishIdToMealId&quot;: {
            &quot;1228578&quot;: 474602,
            &quot;1228585&quot;: 474602,
            &quot;1228586&quot;: 474602,
        ...... // more fields
}

I'm trying to read the &lt;date, number&gt; pairs under dateToDayId into a HashMap&lt;Date, Long&gt; in java. First I created a JsonInfo class which holds a Calendar class object. The Calendar class object in turns holds all the classes including DateToDayId, DishToMealId and so on. My DateToDayId class looks like this (I'm using Jackson to parse JSON file):

package jsonfields;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import deserializers.CustomDateDeserializer;

import java.util.Date;
import java.util.TreeMap;

public class DateToDayId {
    @JsonDeserialize(using = CustomDateDeserializer.class)
    private TreeMap &lt;Date, Long&gt; dateToDayMappings;

    public TreeMap&lt;Date, Long&gt; getDateToDayMappings() {
        return dateToDayMappings;
    }

    public void setDateToDayMappings(TreeMap&lt;Date, Long&gt; dateToDayMappings) {
        this.dateToDayMappings = dateToDayMappings;
    }

    @Override
    public String toString() {
        return &quot;DateToDayId{&quot; +
                &quot;dateToDayMappings=&quot; + dateToDayMappings +
                &#39;}&#39;;
    }
}

My Main class looks like this:

import java.io.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(&quot;path\\to\\file\\test.json&quot;));
            JsonInfo jsonInfo = objectMapper.readValue(inputStreamReader, JsonInfo.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

When I run it, I get the following exception:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field &quot;2016-07-14&quot; (class jsonfields.DateToDayId), not marked as ignorable (one known property: &quot;dateToDayMappings&quot;])
 at [Source: (InputStreamReader); line: 1, column: 48] (through reference chain: JsonInfo[&quot;calendar&quot;]-&gt;jsonfields.Calendar[&quot;dateToDayId&quot;]-&gt;jsonfields.DateToDayId[&quot;2016-07-14&quot;])

How can I fix this?

答案1

得分: 1

你的类映射有误。你需要在 Calendar 类中拥有名为 dateToDayId 的变量名映射。下面修改过的代码应该适用于你的需求。

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.TreeMap;

public class JSONMapTest {
    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("C:\\ws\\test\\test.json"));
            JsonInfo jsonInfo = objectMapper.readValue(inputStreamReader, JsonInfo.class);
            System.out.println(jsonInfo);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class JsonInfo {
    Calendar calendar;

    public Calendar getCalendar() {
        return calendar;
    }

    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
    }
}

class Calendar {
    private TreeMap<Date, Long> dateToDayId;

    public TreeMap<Date, Long> getDateToDayId() {
        return dateToDayId;
    }

    public void setDateToDayId(TreeMap<Date, Long> dateToDayId) {
        this.dateToDayId = dateToDayId;
    }

    @Override
    public String toString() {
        return "DateToDayId{" +
                "dateToDayMappings=" + dateToDayId +
                '}';
    }
}
英文:

Your class mapping is wrong. You need to have map with variable name dateToDayId in Calendar. Below modified code should work for your need.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.TreeMap;
public class JSONMapTest {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
try {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(&quot;C:\\ws\\test\\test.json&quot;));
JsonInfo jsonInfo = objectMapper.readValue(inputStreamReader, JsonInfo.class);
System.out.println(jsonInfo);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class JsonInfo{
Calendar calendar;
public Calendar getCalendar() {
return calendar;
}
public void setCalendar(Calendar calendar) {
this.calendar = calendar;
}
}
class Calendar {
private TreeMap &lt;Date, Long&gt; dateToDayId;
public TreeMap&lt;Date, Long&gt; getDateToDayId() {
return dateToDayId;
}
public void setDateToDayId(TreeMap&lt;Date, Long&gt; dateToDayId) {
this.dateToDayId = dateToDayId;
}
@Override
public String toString() {
return &quot;DateToDayId{&quot; +
&quot;dateToDayMappings=&quot; + dateToDayId +
&#39;}&#39;;
}
}

huangapple
  • 本文由 发表于 2020年9月10日 00:23:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63815729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定