java - 将 Json 字符串传递给对象
问题描述
我已经被困在同一个问题上两周了,所以任何帮助都将不胜感激。
我正在尝试解析 API 的响应,但无论我尝试什么,我都会遇到错误。我希望能够解析字符串并将每个文件提取到对象中,以便稍后在 GUI 中使用它们并对它们进行分类。
以下是响应示例:
{
"filings": [
{
"id": "fil_ol1QrN",
"filing_date": {
"year": 2019,
"month": 10,
"day": 9
},
"accepted_date": {
"dateTime": {
"date": {
"year": 2019,
"month": 10,
"day": 9
},
"time": {
"hour": 18,
"minute": 32,
"second": 27,
"nano": 0
}
},
"offset": {
"totalSeconds": 0
}
},
"period_end_date": {
"year": 2019,
"month": 10,
"day": 7
},
"report_type": "4",
"sec_unique_id": "0000320193-19-000109",
"filing_url": "https://www.sec.gov/Archives/edgar/data/320193/000032019319000109/0000320193-19-000109-index.htm",
"report_url": "https://www.sec.gov/Archives/edgar/data/320193/000032019319000109/xslF345X03/wf-form4_157066032478147.xml"
},
{
"id": "fil_6GoeOv",
"filing_date": {
"year": 2019,
"month": 10,
"day": 3
},
"accepted_date": {
"dateTime": {
"date": {
"year": 2019,
"month": 10,
"day": 3
},
"time": {
"hour": 18,
"minute": 31,
"second": 27,
"nano": 0
}
},
"offset": {
"totalSeconds": 0
}
},
"period_end_date": {
"year": 2019,
"month": 10,
"day": 1
},
"report_type": "4",
"sec_unique_id": "0000320193-19-000106",
"filing_url": "https://www.sec.gov/Archives/edgar/data/320193/000032019319000106/0000320193-19-000106-index.htm",
"report_url": "https://www.sec.gov/Archives/edgar/data/320193/000032019319000106/xslF345X03/wf-form4_157014186524822.xml"
},
{
"id": "fil_Vb3JD0",
"filing_date": {
"year": 2019,
"month": 10,
"day": 3
},
"accepted_date": {
"dateTime": {
"date": {
"year": 2019,
"month": 10,
"day": 3
},
"time": {
"hour": 18,
"minute": 33,
"second": 22,
"nano": 0
}
},
"offset": {
"totalSeconds": 0
}
},
"period_end_date": {
"year": 2019,
"month": 10,
"day": 1
},
"report_type": "4",
"sec_unique_id": "0000320193-19-000107",
"filing_url": "https://www.sec.gov/Archives/edgar/data/320193/000032019319000107/0000320193-19-000107-index.htm",
"report_url": "https://www.sec.gov/Archives/edgar/data/320193/000032019319000107/xslF345X03/wf-form4_157014197957378.xml"
}
],
"company": {
"id": "com_NX6GzO",
"ticker": "AAPL",
"name": "Apple Inc",
"lei": "HWUPKR0MPOU8FGXBT394",
"cik": "0000320193"
},
"next_page": "MjAxOS0xMC0wM3w1NzcwMTQ0"
}
在此,我提取了 3 个不同的文件,并尝试提取每个文件的 id 和报告类型,以使用我在另一个类似的 SO 问题中采用的以下代码开始:
import java.util.HashMap;
import java.util.Map;
public class ExtractSECFilings {
private Map<String, MyObject> filings = new HashMap<String, MyObject>();
public Map<String, MyObject> getfilings() {
return filings;
}
public void setFilings(Map<String, MyObject> filings) {
this.filings = filings;
}
@Override
public String toString() {
return "filings{" +
"filings=" + filings +
'}';
}
}
class MyObject {
private String id;
private String report_type;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getReportType() {
return report_type;
}
public void setReportType(String report_type) {
this.report_type = report_type;
}
@Override
public String toString() {
return "MyObject{" +
"id='" + id + '\'' +
", report_type='" + report_type + '\'' +
'}';
}
}
现在在主要方法中:
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.intrinio.api.*;
import com.intrinio.models.*;
import com.intrinio.invoker.*;
import com.intrinio.invoker.auth.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.Proxy.Type;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.threeten.bp.*;
public class Main {
public static void main(String[] args) throws JsonGenerationException {
ApiClient defaultClient = Configuration.getDefaultApiClient();
ApiKeyAuth auth = (ApiKeyAuth) defaultClient.getAuthentication("ApiKeyAuth");
auth.setApiKey("INSERT_API_KEY");
CompanyApi companyApi = new CompanyApi();
String identifier = "AAPL";
String reportType = null;
LocalDate startDate = null;
LocalDate endDate = null;
Integer pageSize = 3;
String nextPage = null;
try {
ApiResponseCompanyFilings result = companyApi.getCompanyFilings(identifier, reportType, startDate, endDate,
pageSize, nextPage);
String convertedResult = new Gson().toJson(result);
System.out.println(convertedResult);
ObjectMapper mapper = new ObjectMapper();
ExtractSECFilings readValue = mapper.readValue(convertedResult, ExtractSECFilings.class);
System.out.println("readValue = " + readValue);
} catch (ApiException | IOException e) {
System.err.println("Exception when calling CompanyApi#getCompanyFilings");
e.printStackTrace();
}
}
}
我收到以下错误:
调用 CompanyApi#getCompanyFilings com.fasterxml.jackson.databind.exc.MismatchedInputException 时出现异常:无法反序列化
java.util.LinkedHashMap
[Source: (String)"{"filings":[{"id":"fil_ol1QrN","归档日期":{"年":2019,"月":10,"日":9},"接受日期":{"日期时间":{"日期":{"年":2019,"月":10, "day":9},"time":{"hour":18,"minute":32,"second":27,"nano":0}},"offset":{"totalSeconds":0}} ,"period_end_date":{"year":2019,"month":10,"day":7},"report_type":"4","sec_unique_id":"0000320193-19-000109","归档网址“:” https://www.sec.gov/Archives/edgar/data/320193/000032019319000109/0000320193-19-000109-index.htm “,报告网址”:” https://www.sec.gov/档案/埃德加/达"[截断 1342 个字符];行:1,列:12](通过引用链:jsonToObjects.ExtractSECFilings["filings"])
我已经尝试过很多教程并不断收到错误。我是编程新手,所以再次感谢任何帮助。
解决方案
它无法将您的 pojo 属性与您的 json 字符串匹配。尝试更改private Map<String, MyObject> filings = new HashMap<String, MyObject>();
为private List<MyObject> filings = new ArrayList<>();
UPD:如果发生,只需在类上com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
添加@JsonIgnoreProperties(ignoreUnknown = true)
注释。ExtractSECFilings
@JsonIgnoreProperties(ignoreUnknown = true)
static class ExtractSECFilings {}
推荐阅读
- java - Java ZonedDateTime 最小/最大偏移量
- solace - 未在 solAdmin 中查看安慰消息 Id
- sql - 执行我的基本查询需要很长时间
- vue.js - Meteor vue 跟踪器未获取反应数据
- android - Player$OnAudioFocusChangedListener)' 在空对象引用上
- python - 如何让硒驱动程序等待某些情况?
- c# - 删除小数点后的值
- c# - 为什么 ConcurrentDictionary 有 AddOrUpdate 和 GetOrAdd,而 Dictionary 没有?
- php - 读取多个 Json 文件并在 PHP 中求和一个键
- python - Pandas 0.24 核心标题样式不起作用