开发者社区> 问答> 正文

将Json文件写入从页面提取的数据

我正在从页面https://br.investing.com/indices/major-indices上的表中提取数据

我可以完美地提取数据,但是我不知道如何以以下格式写入Json。

用于数据提取的代码:

public void AcessarPagina() {
    System.out.println("*** Abrindo a Pagina de Extração *** \n");
    driver.get("https://br.investing.com/indices/major-indices");
    WebDriverWait AguardarTabela = new WebDriverWait(driver, 20);
    AguardarTabela.until(ExpectedConditions.presenceOfElementLocated(By
                                           .id("cross_rates_container")));
    System.out.println("*** Iniciando a Extração *** \n");
    WebElement TabelaCabecalho = driver.findElement(By.id("cross_rates_container"));
    List<WebElement> Cabecalho = TabelaCabecalho.findElements(By.tagName("th"));
    for (int i = 0; i < Cabecalho.size(); i++) {
        System.out.print(Cabecalho.get(i).getText() + ";");
    }
    WebElement TabelaDados = driver.findElement(By.id("cross_rates_container"));
    List<WebElement> Linhas = TabelaDados.findElements(By.tagName("tr"));
    List<WebElement> ListaColunas = null;
    for (WebElement row : Linhas) {
        System.out.println();
        ListaColunas = row.findElements(By.tagName("td"));
        for (WebElement column : ListaColunas) {
            System.out.print(column.getText() + ";");
        }
    }
}

我需要的Json格式:


[ {
  "indice" : "Dow 30",
  "ultimo" : "23.185,62",
  "maxima" : "23.189,76",
  "minima" : "21.285,37",
  "variacao" : "+1.985,00",
  "variacao2" : "+9,36%",
  "hora" : "13/03"
}, {
  "indice" : "S&P 500 VIX",
  "ultimo" : "23.185,62",
  "maxima" : "23.189,76",
  "minima" : "21.285,37",
  "variacao" : "+1.985,00",
  "variacao2" : "+9,36%",
  "hora" : "13/03"
} ]

问题来源:Stack Overflow

展开
收起
montos 2020-03-26 08:57:08 492 0
1 条回答
写回答
取消 提交回答
  • 将对象映射到Java对象,然后使用Chris提到的Jackson可能是最好的选择。我整理了一个示例,说明您可以做什么:

    这是可以保存您的值的Java对象:

    public class TableElement {
        private String indice;
        private String ultimo;
        private String maxima;
        private String minima;
        private String variacao;
        private String variacao2;
        private String hora;
    
        public String getIndice() {
            return indice;
        }
    
        public void setIndice(String indice) {
            this.indice = indice;
        }
    
        // Add all getters and setters ...
    }
    

    注意-需要使用getter和setter方法,因为这是杰克逊用来从/向对象检索/设置对象值的方法 json

    并将其实际映射到json这样的地方:

    StockTable stockTable = new StockTable();
    List<TableElement> stocks = new ArrayList<>();
    
    TableElement tableElement = new TableElement();
    tableElement.setIndice("Dow 30");
    tableElement.setUltimo("23.185,62");
    tableElement.setMaxima("23.189,76");
    tableElement.setMinima("21.285,37");
    tableElement.setVariacao("+1.985,00");
    tableElement.setVariacao2("+9,36%");
    tableElement.setHora("13/03");
    
    TableElement tableElement2 = new TableElement();
    tableElement2.setIndice("S&P 500 VIX");
    tableElement2.setUltimo("23.185,62");
    tableElement2.setMaxima("23.189,76");
    tableElement2.setMinima("21.285,37");
    tableElement2.setVariacao("+1.985,00");
    tableElement2.setVariacao2("+9,36%");
    tableElement2.setHora("13/03");
    
    stocks.add(tableElement);
    stocks.add(tableElement2);
    
    ObjectMapper objectMapper = new ObjectMapper();
    
    try {
        System.out.println(objectMapper.writerWithDefaultPrettyPrinter()
                                           .writeValueAsString(stocks));
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    

    它将产生如下所示的输出:

    [ {
      "indice" : "Dow 30",
      "ultimo" : "23.185,62",
      "maxima" : "23.189,76",
      "minima" : "21.285,37",
      "variacao" : "+1.985,00",
      "variacao2" : "+9,36%",
      "hora" : "13/03"
    }, {
      "indice" : "S&P 500 VIX",
      "ultimo" : "23.185,62",
      "maxima" : "23.189,76",
      "minima" : "21.285,37",
      "variacao" : "+1.985,00",
      "variacao2" : "+9,36%",
      "hora" : "13/03"
    } ]
    

    回答来源:Stack Overflow

    2020-03-26 08:58:04
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载