让Sphider输出JSON

时间:2012-06-04 21:34:24

标签: json sphider

我最近将Sphider抓取工具添加到了我的网站,以便添加搜索功能。但是我下载的Sphider发行版附带的默认search.php太简单了,并且与我网站的其余部分没有很好的集成。我在网站顶部有一个小导航栏,其中有一个搜索框,我希望能够使用Ajax通过该搜索字段访问Sphider的搜索结果。为此,我想我需要让Sphider以JSON格式返回结果。

我这样做的方式是我使用了一个输出JSON的“主题”(Sphider支持“主题”输出)。我在Sphider网站的this thread找到了这个主题。 似乎可以工作,但更严格的JSON解析器不会解析它。这是JSON输出的一些示例:

{"result_report":"Displaying results 1 - 1 of 1 match (0 seconds) ", "results":[ { "idented":"false", "num":"1", "weight":"[100.00%]", "link":"http://www.avtainsys.com/articles/Triple_Contraints", "title":"Triple Contraints", "description":" on 01/06/12 Project triple constraints are time, cost, and quality. These are the three constraints that control the performance of the project. Think about this triple-constraint as a three-leg tripod. If one of the legs is elongated or", "link2":"http://www.avtainsys.com/articles/Triple_Contraints", "size":"3.3kb" }, { "num":"-1" } ], "other_pages":[ { "title":"1", "link":"search.php?query=constraints&start=1&search=1&results=10&type=and&domain=", "active":"true" }, ] }

问题是在结尾附近有一个尾随的逗号。根据{{​​3}},当使用PHP的json_decode()函数时,“不允许使用尾随逗号”。此JSON也无法使用this在线格式化程序进行解析。但是当我拿出逗号时,它起了作用,我得到了格式更好的JSON:

{
   "result_report":"Displaying results 1 - 1 of 1 match (0 seconds) ",
   "results":[
      {
         "idented":"false",
         "num":"1",
         "weight":"[100.00%]",
         "link":"http://www.avtainsys.com/articles/Triple_Contraints",
         "title":"Triple Contraints",
         "description":" on 01/06/12 Project triple constraints are time, cost, and quality. These are the three constraints that control the performance of the project. Think about this triple-constraint as a three-leg tripod. If one of the legs is elongated or",
         "link2":"http://www.avtainsys.com/articles/Triple_Contraints",
         "size":"3.3kb"
      },
      {
         "num":"-1"
      }
   ],
   "other_pages":[
      {
         "title":"1",
         "link":"search.php?query=constraints&start=1&search=1&results=10&type=and&domain=",
         "active":"true"
      }
   ]
}

现在,我将如何以编程方式执行此操作?而且(也许更重要的是),是否有更优雅的方式来实现这一目标?你应该知道PHP是我可以在我的共享主机帐户上运行的唯一语言,因此Java解决方案对我来说不起作用。

1 个答案:

答案 0 :(得分:2)

search_result.html中,您可以在foreach循环的末尾包围,条件,只有在索引严格小于页数-1时才打印。