英文:
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
类对象依次包含了DateToDayId
、DishToMealId
等所有类。我的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:
{
"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,
...... // more fields
}
I'm trying to read the <date, number>
pairs under dateToDayId
into a HashMap<Date, Long>
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 <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 +
'}';
}
}
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("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();
}
}
}
When I run it, I get the following exception:
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"])
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("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 +
'}';
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论