json JSoup文档对象,在下面复杂的html元素的情况下如何解析数据?

6yt4nkrj  于 2023-02-26  发布在  其他
关注(0)|答案(2)|浏览(128)

网址:https://www.moneycontrol.com/india/stockpricequote/auto-23-wheelers/bajajauto/BA10
我正在获取下面的文档doc.select(“#C-12-graph”)。get(0)

<div id="C-12-graph" style="display: none;">[{"heading":"Revenue","data":[{"year":"2018","value":"25218","formattedValue":"25,218 Cr"},{"year":"2019","value":"30357","formattedValue":"30,357 Cr"},{"year":"2020","value":"29918","formattedValue":"29,918 Cr"},{"year":"2021","value":"27741","formattedValue":"27,741 Cr"},{"year":"2022","value":"33144","formattedValue":"33,144 Cr"}]},{"heading":"Net Profit","data":[{"year":"2018","value":"3931","formattedValue":"3,931 Cr"},{"year":"2019","value":"4577","formattedValue":"4,577 Cr"},{"year":"2020","value":"4890","formattedValue":"4,890 Cr"},{"year":"2021","value":"4550","formattedValue":"4,550 Cr"},{"year":"2022","value":"5586","formattedValue":"5,586 Cr"}]},{"heading":"EPS","data":[{"year":"2018","value":"145.80","formattedValue":"145.80"},{"year":"2019","value":"170.30","formattedValue":"170.30"},{"year":"2020","value":"180.20","formattedValue":"180.20"},{"year":"2021","value":"167.90","formattedValue":"167.90"},{"year":"2022","value":"213.20","formattedValue":"213.20"}]},{"heading":"BVPS","data":[{"year":"2018","value":"705.85","formattedValue":"705.85"},{"year":"2019","value":"802.91","formattedValue":"802.91"},{"year":"2020","value":"748.59","formattedValue":"748.59"},{"year":"2021","value":"942.51","formattedValue":"942.51"},{"year":"2022","value":"1031.89","formattedValue":"1,031.89"}]},{"heading":"ROE","data":[{"year":"2018","value":"20.65","formattedValue":"20.65"},{"year":"2019","value":"21.20","formattedValue":"21.20"},{"year":"2020","value":"24.06","formattedValue":"24.06"},{"year":"2021","value":"17.80","formattedValue":"17.80"},{"year":"2022","value":"20.64","formattedValue":"20.64"}]},{"heading":"Debt to Equity","data":[{"year":"2018","value":"0.00","formattedValue":"0.00"},{"year":"2019","value":"0.00","formattedValue":"0.00"},{"year":"2020","value":"0.00","formattedValue":"0.00"},{"year":"2021","value":"0.00","formattedValue":"0.00"},{"year":"2022","value":"0.00","formattedValue":"0.00"}]}]</div>

从这个我想得到的数据低于标题值收入,净利润等,并低于他们每年的数据值
例如,标题=收入,2018年= 25218,2019年= 30357
请澄清如何使用Jsoup文档对象执行此操作?

lsmepo6l

lsmepo6l1#

你所要求的不能使用jsoup来完成,你必须使用json解析器。
如果您运行以下代码(使用正确的URL)-

Document doc = Jsoup.connect(url).get();        
Element data = doc.select("#C-12-graph").get(0);
String jsonArray = data.text();

那么jsonArray的内容实际上是一个json数组,jsoup将其作为纯文本处理。
您可以使用下面的示例,该示例使用了java-json库(可以在here中找到,但还有很多其他解析器)-

for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jsonobject = jsonArray.getJSONObject(i);
    String name = jsonobject.getString("heading");
    System.out.println(name);
}

而输出为-
收入
净利润
每股收益
BVPS系统
净资产收益率
债务对权益比率
同样的方法,您可以解析您需要的所有详细信息。

00jrzges

00jrzges2#

参考文献:
Extracting data from JSON array
How to get JSON data from HTML using jsoup
A JSONObject text must begin with '{' at 1 [character 2 line 1] with '{' error
How to Remove Square bracket from JSON
https://www.jsonformatter.io/

String cssQueryToConsolidatedFinancials = "#C-12-graph";
            String year, revenue;

            System.out.println("Net Profit Last 5 years");

            JSONObject revenueJSONObj = new JSONArray( doc.selectFirst(cssQueryToConsolidatedFinancials).text() ).getJSONObject(1);

            if(revenueJSONObj.getString("heading").equals("Net Profit") ){
                for( int cnt = 0; cnt < revenueJSONObj.getJSONArray("data").length(); cnt++){
                    year = revenueJSONObj.getJSONArray("data").getJSONObject(cnt).getString("year");
                    revenue = revenueJSONObj.getJSONArray("data").getJSONObject(cnt).getString("value");
                    System.out.println(year + "  " + revenue);
                }
            }else{
                System.out.println("ERROR");
            }

            System.out.println("STOP");

        }catch(Exception e){
            e.printStackTrace();
        }

相关问题