我一直在尝试使用jquery解析phpunit json日志但是它返回错误。
我使用以下代码解析
<html>
<head>
<title>PHPUnit Report</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="./jquery-1.6.2.min.js"></script>
</head>
<body>
<script type="text/javascript">
var json = $.getJSON('logfile.json', function(data){
alert(data);
}).success(function() { alert("second success"); })
.error(function() { alert("error"); });
</script>
</body>
</html>
这是json日志文件
{"event":"suiteStart","suite":"","tests":22}{"event":"suiteStart","suite":"Application_Model_CarrinhoTest","tests":4}{"event":"testStart","suite":"Application_Model_CarrinhoTest","test":"Application_Model_CarrinhoTest::testCriacaoObjeto"}{"event":"test","suite":"Application_Model_CarrinhoTest","test":"Application_Model_CarrinhoTest::testCriacaoObjeto","status":"pass","time":0.013954162597656,"trace":[],"message":""}{"event":"testStart","suite":"Application_Model_CarrinhoTest","test":"Application_Model_CarrinhoTest::testAdicionarProduto"}{"event":"test","suite":"Application_Model_CarrinhoTest","test":"Application_Model_CarrinhoTest::testAdicionarProduto","status":"pass","time":0.015092134475708,"trace":[],"message":""}{"event":"testStart","suite":"Application_Model_CarrinhoTest","test":"Application_Model_CarrinhoTest::testRemoverProduto"}{"event":"test","suite":"Application_Model_CarrinhoTest","test":"Application_Model_CarrinhoTest::testRemoverProduto","status":"pass","time":0.0048158168792725,"trace":[],"message":""}{"event":"testStart","suite":"Application_Model_CarrinhoTest","test":"Application_Model_CarrinhoTest::testLimparCarrinho"}{"event":"test","suite":"Application_Model_CarrinhoTest","test":"Application_Model_CarrinhoTest::testLimparCarrinho","status":"pass","time":0.0047218799591064,"trace":[],"message":""}{"event":"suiteStart","suite":"Application_Model_ProdutoTest","tests":3}{"event":"testStart","suite":"Application_Model_ProdutoTest","test":"Application_Model_ProdutoTest::testaCriacaoProduto"}{"event":"test","suite":"Application_Model_ProdutoTest","test":"Application_Model_ProdutoTest::testaCriacaoProduto","status":"pass","time":0.0066261291503906,"trace":[],"message":""}{"event":"testStart","suite":"Application_Model_ProdutoTest","test":"Application_Model_ProdutoTest::testaErroCriacao"}{"event":"test","suite":"Application_Model_ProdutoTest","test":"Application_Model_ProdutoTest::testaErroCriacao","status":"pass","time":0.0081219673156738,"trace":[],"message":""}{"event":"testStart","suite":"Application_Model_ProdutoTest","test":"Application_Model_ProdutoTest::testaCadastroProduto"}{"event":"test","suite":"Application_Model_ProdutoTest","test":"Application_Model_ProdutoTest::testaCadastroProduto","status":"pass","time":0.086114883422852,"trace":[],"message":""}{"event":"suiteStart","suite":"Default_CarrinhoControllerTest","tests":4}{"event":"testStart","suite":"Default_CarrinhoControllerTest","test":"Default_CarrinhoControllerTest::testIndexAction"}{"event":"test","suite":"Default_CarrinhoControllerTest","test":"Default_CarrinhoControllerTest::testIndexAction","status":"fail","time":0.18276596069336,"trace":[],"message":"Failed asserting node denoted by div#view-content p CONTAINS content \"View script for controller <b>Carrinho<\/b> and script\/action name <b>index<\/b>\""}{"event":"testStart","suite":"Default_CarrinhoControllerTest","test":"Default_CarrinhoControllerTest::testAdicionarProdutoAction"}{"event":"test","suite":"Default_CarrinhoControllerTest","test":"Default_CarrinhoControllerTest::testAdicionarProdutoAction","status":"pass","time":0.038674831390381,"trace":[],"message":""}{"event":"testStart","suite":"Default_CarrinhoControllerTest","test":"Default_CarrinhoControllerTest::testRemoverProdutoAction"}{"event":"test","suite":"Default_CarrinhoControllerTest","test":"Default_CarrinhoControllerTest::testRemoverProdutoAction","status":"pass","time":0.039582967758179,"trace":[],"message":""}{"event":"testStart","suite":"Default_CarrinhoControllerTest","test":"Default_CarrinhoControllerTest::testAlterarQuantidadeProdutoAction"}{"event":"test","suite":"Default_CarrinhoControllerTest","test":"Default_CarrinhoControllerTest::testAlterarQuantidadeProdutoAction","status":"pass","time":0.038434982299805,"trace":[],"message":""}{"event":"suiteStart","suite":"Default_CategoriaControllerTest","tests":2}{"event":"testStart","suite":"Default_CategoriaControllerTest","test":"Default_CategoriaControllerTest::testIndexAction"}{"event":"test","suite":"Default_CategoriaControllerTest","test":"Default_CategoriaControllerTest::testIndexAction","status":"fail","time":0.043931007385254,"trace":[],"message":"Failed asserting node denoted by div#view-content p CONTAINS content \"View script for controller <b>Categoria<\/b> and script\/action name <b>index<\/b>\""}{"event":"testStart","suite":"Default_CategoriaControllerTest","test":"Default_CategoriaControllerTest::testDetalheAction"}{"event":"test","suite":"Default_CategoriaControllerTest","test":"Default_CategoriaControllerTest::testDetalheAction","status":"pass","time":0.040285110473633,"trace":[],"message":""}{"event":"suiteStart","suite":"Default_IndexControllerTest","tests":1}{"event":"testStart","suite":"Default_IndexControllerTest","test":"Default_IndexControllerTest::testIndexAction"}{"event":"test","suite":"Default_IndexControllerTest","test":"Default_IndexControllerTest::testIndexAction","status":"fail","time":0.045408964157104,"trace":[],"message":"Failed asserting node denoted by div#welcome h3 CONTAINS content \"This is your project's main page\""}{"event":"suiteStart","suite":"Default_ProdutoControllerTest","tests":2}{"event":"testStart","suite":"Default_ProdutoControllerTest","test":"Default_ProdutoControllerTest::testIndexAction"}{"event":"test","suite":"Default_ProdutoControllerTest","test":"Default_ProdutoControllerTest::testIndexAction","status":"fail","time":0.04747200012207,"trace":[],"message":"Failed asserting last controller used <\"error\"> was \"Produto\""}{"event":"testStart","suite":"Default_ProdutoControllerTest","test":"Default_ProdutoControllerTest::testDetalheAction"}{"event":"test","suite":"Default_ProdutoControllerTest","test":"Default_ProdutoControllerTest::testDetalheAction","status":"fail","time":0.042054891586304,"trace":[],"message":"Failed asserting last controller used <\"error\"> was \"Produto\""}{"event":"suiteStart","suite":"Default_ProdutosControllerTest","tests":3}{"event":"testStart","suite":"Default_ProdutosControllerTest","test":"Default_ProdutosControllerTest::testIndexAction"}{"event":"test","suite":"Default_ProdutosControllerTest","test":"Default_ProdutosControllerTest::testIndexAction","status":"pass","time":0.046010971069336,"trace":[],"message":""}{"event":"testStart","suite":"Default_ProdutosControllerTest","test":"Default_ProdutosControllerTest::testBuscarAction"}{"event":"test","suite":"Default_ProdutosControllerTest","test":"Default_ProdutosControllerTest::testBuscarAction","status":"pass","time":0.043781995773315,"trace":[],"message":""}{"event":"testStart","suite":"Default_ProdutosControllerTest","test":"Default_ProdutosControllerTest::testDetalheAction"}{"event":"test","suite":"Default_ProdutosControllerTest","test":"Default_ProdutosControllerTest::testDetalheAction","status":"pass","time":0.04433798789978,"trace":[],"message":""}{"event":"suiteStart","suite":"Default_UsuarioControllerTest","tests":3}{"event":"testStart","suite":"Default_UsuarioControllerTest","test":"Default_UsuarioControllerTest::testIndexAction"}{"event":"test","suite":"Default_UsuarioControllerTest","test":"Default_UsuarioControllerTest::testIndexAction","status":"pass","time":0.048629999160767,"trace":[],"message":""}{"event":"testStart","suite":"Default_UsuarioControllerTest","test":"Default_UsuarioControllerTest::testLogarAction"}{"event":"test","suite":"Default_UsuarioControllerTest","test":"Default_UsuarioControllerTest::testLogarAction","status":"pass","time":0.046178817749023,"trace":[],"message":""}{"event":"testStart","suite":"Default_UsuarioControllerTest","test":"Default_UsuarioControllerTest::testSairAction"}{"event":"test","suite":"Default_UsuarioControllerTest","test":"Default_UsuarioControllerTest::testSairAction","status":"pass","time":0.04793381690979,"trace":[],"message":""}
怎么了?我怎么解析它?
答案 0 :(得分:2)
我认为你的JSON应该是它应该是的,phpunit在事件发生时输出JSON ......这些事件中的每一个都可以很容易地被解析,但是将它们全部作为一个会抛出错误。
我也希望有一种格式可以直接在JS中解析或使用php的json_decode,但看起来现在不会发生......
答案 1 :(得分:0)
唯一缺少的是事件和包装方括号之间的逗号。您可以手动添加它们,然后添加json_decode。
$json = file_get_contents('/log.json');
$json = '['.str_replace('}{', '},{', $json).']';
$data = json_decode($json, true);
答案 2 :(得分:0)
您可以事先使用sed
将json文件转换为有效文件:
sed -i.bak 's/^{$/[{/g;s/^}$/}]/g;s/}{/},{/g' logfile.json