【问题标题】:How to parse javascript variable in Java如何在Java中解析javascript变量
【发布时间】:2013-11-20 19:43:41
【问题描述】:

页面包含 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”?

谢谢。

【问题讨论】:

  • 此时,你有没有从脚本中提取“json_structure”?
  • @Slihp 不,我已经提取了所有的 JavaScript 代码,但我不知道如何从中提取“json_structure”。
  • 我可能会使用 JSON 正则表达式来提取数据。见stackoverflow.com/questions/2583472/regex-to-validate-json

标签: java javascript json jsoup


【解决方案1】:

这是最简单的方法:

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_结构

:)

【讨论】:

    猜你喜欢
    • 2013-07-29
    • 2019-06-01
    • 2013-08-24
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多