我一直在同一个问题上待了两个星期,因此对您的帮助将不胜感激。
我正在尝试解析API的响应,但是无论尝试如何,都会不断出错。我希望能够解析String并将每个文件提取到对象,以便以后可以在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:无法 在START_ARRAY中反序列化
java.util.LinkedHashMap
的实例 [来源: (字符串)“ {” 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/da“” [被截断 1342个字符];行:1,列:12](通过参考链: jsonToObjects.ExtractSECFilings [“ filings”])
我尝试了许多教程,并不断出错。我是编程的新手,所以再次感谢您的帮助。
答案 0 :(得分:0)
它无法将您的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 {}
答案 1 :(得分:0)
我从上面的json猜测,您正在尝试获取json节点“ id”和“ report_type”
@JsonIgnoreProperties(ignoreUnknown=true)
class Wrapper{
@JsonProperty(value="filings")
List<ExtractSECFilings> filings;
public List<ExtractSECFilings> getFillings() {
return filings;
}
public void setFillings(List<ExtractSECFilings> fillings) {
this.filings = fillings;
}
}
@JsonIgnoreProperties(ignoreUnknown=true)
class ExtractSECFilings {
String id;
String report_type;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getReport_type() {
return report_type;
}
public void setReport_type(String report_type) {
this.report_type = report_type;
}
@Override
public String toString() {
return "ExtractSECFilings [id=" + id + ", report_type=" + report_type + "]";
}
}
//here add this code in your main application to serialize the json
public class Demo{
public static void main(String args[]){
Wrapper wrapper=mapper.readValue(val, Wrapper.class);
System.out.println(wrapper.getFillings());
}
}
```
in the console you can see that id and report type json node values