Java 获取json和字符串格式的Har数据

Java 获取json和字符串格式的Har数据,java,selenium-webdriver,har,browsermob-proxy,Java,Selenium Webdriver,Har,Browsermob Proxy,我编写了一个从firefox浏览器获取har数据的类 我想得到JSON格式的har数据以正确显示 我的代码: ProxyServer server = new ProxyServer(4444); server.start(); //captures the moouse movements and navigations server.setCaptureHeaders(true); server.setCaptureContent(true); /

我编写了一个从firefox浏览器获取har数据的类

我想得到JSON格式的har数据以正确显示

我的代码:

ProxyServer server = new ProxyServer(4444);
    server.start();

    //captures the moouse movements and navigations
    server.setCaptureHeaders(true);
    server.setCaptureContent(true);

    // get the Selenium proxy object
    Proxy proxy = server.seleniumProxy();

    // configure it as a desired capability
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.PROXY, proxy);

    // start the browser up
    WebDriver driver = new FirefoxDriver(capabilities);

    // create a new HAR with the label "apple.com"
    server.newHar("yahoo.com");

    // open yahoo.com
    driver.get("http://yahoo.com");

    // get the HAR data
    net.lightbody.bmp.core.har.Har har = server.getHar();

请任何人帮助我获取JSON格式和字符串的HAR数据

很抱歉回答晚了

希望它能帮助这里的其他人

您可以使用StringWriter将Har写入字符串。所以你可以得到haras字符串。并且,您可以使用Gson解析该字符串以获得JSON对象

net.lightbody.bmp.core.har.Har har = server.getHar();

java.io.StringWriter writer = new java.io.StringWriter();
try {
            har.writeTo(writer);
        } catch (IOException e) {
            e.printStackTrace();
        }

String harAsString = writer.toString();

com.google.gson.JsonElement harAsJson = new com.google.gson.Gson().toJsonTree(writer.toString());