【发布时间】:2022-12-12 11:46:41
【问题描述】:
我是 ndJSON 的新手。我有一个包含以下数据的 ndJSON 文件:
{
"Top-level":{
"customer_no":"1",
"created":"2019-03-27 14:24:54",
"last_visited_day_time":null,
"login":"roni_cost@example.com"
},
"profile":{
"salutation":"Mr",
"title":null,
"company":null,
"job_title":null,
"first_name":"Veronica",
"last_name":"Costello",
"name_suffix":"NONE",
"gender":"Female",
"birthday":"1973-12-15",
"email":"roni_cost@example.com",
"next_birthday":"2022-12-15",
"second_name":null
},
"phone":{
"home_phone":null,
"business_phone":null,
"mobile_phone":null,
"fax_number":null
},
"addresses":[
{
"address_id":"1",
"title":"",
"company":null,
"salutation":null,
"first_name":"Veronica",
"last_name":"Costello",
"second_name":null,
"suffix":"NONE",
"address_1":"6146 Honey Bluff Parkway",
"address_2":"",
"suite_no":"",
"postal_box":"",
"city":"Calder",
"postal_code":"49628-7978",
"country":"US",
"state":"Michigan",
"contact_phone":"(555) 229-3326"
}
],
"orders":{
"placed_orders_count":2,
"0":{
"order_id":"000000001",
"order_date":"2019-03-27 14:25:03"
},
"1":{
"order_id":"000000002",
"order_date":"2019-03-27 14:25:03"
}
},
"customs":[
]
}
我想将该文件读入我的程序并从中提取数据。
以下是我从系统中读取文件并尝试将其写入 CustomerDTO 对象的代码
Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader("customer.json"));
customerFeedDTO = gson.fromJson(reader, CustomerFeedDTO.class);
但我收到以下异常:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 138 path $.profile
我的 CustomerFeedDTO 是:
public class CustomerFeedDTO {
private ArrayList<?> topLevel;
private ArrayList<?> profile;
private ArrayList<?> phone;
private ArrayList<?> addresses;
private ArrayList<?> orders;
private ArrayList<?> customs;
//Getters and setters
我正在尝试将 ndJSON 文件中的所有数据映射到我的 customerDTO 对象中
【问题讨论】:
标签: java json java-8 gson ndjson