简体   繁体   中英

Populating JTable with objects pased from Json response

UPDATED

I'm having difficulty parsing the following Json string:

{"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"}],"company":{"id":"com_NX6GzO","ticker":"AAPL","name":"Apple Inc","lei":"HWUPKR0MPOU8FGXBT394","cik":"0000320193"},"next_page":"MjAxOS0xMC0wOXw1Nzc2MDA5"}

Someone on here has helped me get "id" and "report_type", but now I would like the filing_date as well, but it's not in the usual date format. I want to populate a JTable with the objects gotten from the API response.

What would be the best approach to get the date? I've tried making getters and setters for year, month and day but it doesn't work, and since those values are nested I'm not entirely sure how to approach this.

here is the Wrapper class:

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

@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;
String report_url;


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;
}

public String getReport_url(String report_url) {
return report_url;
}
public void setReport_url(String report_url) {
  this.report_url = report_url; 
}


@Override
public String toString() {
return "ExtractSECFilings [id=" + id + ", report_type=" + report_type + ", 
Report url=" + report_url + "]";
}
}

And the main method:

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.json.simple.parser.JSONParser;
import org.threeten.bp.*;

public class intrinioTest {
public static void main(String[] args) throws IOException {

    ApiClient defaultClient = Configuration.getDefaultApiClient();
    ApiKeyAuth auth = (ApiKeyAuth) 
defaultClient.getAuthentication("ApiKeyAuth");
    auth.setApiKey("API_KEY");

    CompanyApi companyApi = new CompanyApi();

    String identifier = "AAPL"; // String | A Company identifier (Ticker, 
 CIK, LEI, Intrinio ID)
    String reportType = null; // String | Filter by report type [see -
                                // /documentation/sec_filing_report_types]. 
   Separate values with commas to
                                // return multiple report types.
    LocalDate startDate = null; // LocalDate | Filed on or after the given 
    date
    LocalDate endDate = null; // LocalDate | Filed before or after the given 
    date
    Integer pageSize = 10; // Integer | The number of results to return
    String nextPage = null; // String | Gets the next page of data from a 
   previous API call

    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();
        Wrapper wrapper=mapper.readValue(convertedResult, Wrapper.class);
        System.out.println(wrapper.getFillings());
        */

    } catch (ApiException e) {
        System.err.println("Exception when calling 
 CompanyApi#getCompanyFilings");
        e.printStackTrace();
    }
   }

 }

Thanks to anyone who can help me get the filing_date.

I'm still trying to figure out how to populate a JTable from the extracted objects. Any help would be greatly apreciated.

Since "filing_date" is a nested object within the Json response, I had to create a class to extract year, month and day.

private class Filing_date {
    int year;
     int month;
     int day;

    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public int getMonth() {
        return month;
    }
    public void setMonth(int month) {
        this.month = month;
    }
    public int getDay() {
        return day;
    }
    public void setDay(int day) {
        this.day = day;
    }

}

And then to see the extracted values I updated the public String toString() like this:

@Override
public String toString() {
  return "[id: " + id + ", report_type: " + report_type + ", Report url: " + report_url + ", Filing Date: " + filing_date.year +"/" + filing_date.month + "/" + filing_date.day + "]";
}

Here is a very useful tutorial that helped me find what I was looking for, hope this helps others.

https://futurestud.io/tutorials/gson-mapping-of-nested-objects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM