如何在Java中解析javascript变量

How to parse javascript variable in Java

本文关键字:javascript 变量 Java      更新时间:2023-09-26

页面包含JavaScript代码:

<script type="text/javascript">
    $(document).ready(function () {
        var myVar = new cinema({json_structure}); 
    });

我使用Jsoup库获得了以下JavaScript代码:

Document doc = Jsoup.connect("http://example.com").timeout(0).get();    
Element script = doc.select("script").get(6);

如何解析"json_structure"?

谢谢。

以下是最简单的方法:

Pattern p = Pattern.compile(REGEX); // Regex for the value of the key
Matcher m = p.matcher(script.html()); // you have to use html here and NOT text! Text will drop the 'key' part

while( m.find() )
{
    System.out.println(m.group(1)); // value
}

在您的情况下,这将输出:

json_结构

:)