简体   繁体   中英

Java: How can I get JSON value from a script tag?

My goal is to get JSON value from a script tag from a web page. The tricky part is the javaScript tag contains lot of functions as well as JSON array. I was able to convert the entire script in string format through jsoup but I'm not sure how to get value of the JSON variable.

here is my Java code

Jsoup.connect("http://www.example.com/").timeout(1000).get();
    Elements scriptElements = doc.getElementsByTag("script");
    for (Element element :scriptElements ){                
        for (DataNode node : element.dataNodes()) {
          System.out.println(node.getWholeData());
          }
    }`

script tag

<script type="text/javascript"> xy = xy ||{}; if(typeof xyz==='hello'){ xy={name:'myName'||'', id:'123', a:function(app,eve,json),1);}}, b:function()....}}</script>

Here I want to get the value of name and id

除了使用子字符串外,您可能还需要考虑使用诸如http://www.antlr.org/之类的工具来解析javascript(它不是javascript,而不是json)

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