Javascript 解析<;的json数据;突出显示>;标记以高亮显示字段中的文本
并将其解析为Javascript 解析<;的json数据;突出显示>;标记以高亮显示字段中的文本,javascript,jquery,json,solr,Javascript,Jquery,Json,Solr,并将其解析为 $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=20&q="+newquery+"&sort=price asc&hl=true&hl.fl=description&hl.usePhraseHighlighter=true&json.wrf=?", function(newresult){ 及 假设响应始终为文本
$.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=20&q="+newquery+"&sort=price asc&hl=true&hl.fl=description&hl.usePhraseHighlighter=true&json.wrf=?", function(newresult){
及
假设响应始终为文本,并且只有要突出显示的元素包含在
标记中,并且只有一个标记,则可以执行以下操作:
$.each(newresult.highlighting, function(i,hitem){
您的JSON无效:
“文档”:[1.{
文档
和突出显示
不是标记,它们是属性。您现在想做什么?请澄清您的问题。@Felix Kling:我已经编辑了JSON,现在它是有效的。我的问题是我想得到“优雅”在我的搜索结果中突出显示,如果我将突出显示属性解析到elegant@FelixKling:现在我的问题是如何进入,搜索结果是哪一次?那些在文档中的?@Felix Kling:是的,搜索结果在文档中Elix Kling:我可以使用hitem.description[0],因为描述在“8252”中,我无法获得此描述:(@Romi:这应该可以正常工作。i
应该是8252
和hitem
对应的元素。请参阅:您使用了JSON.stringify((hitem.description[0]),这是什么。Felix Kling:当我将警报(word)放在$.each(newresult.highlighting,function(i,hitem)中时){,那么我的jsp页面不工作。脚本没有显示任何错误,但它不是running@Romi:我刚刚尝试了一些功能。在没有它的情况下也可以使用:。您必须在定义它之后放置警报(word)
。
$.each(newresult.response.docs, function(i,item){
$.each(newresult.highlighting, function(i,hitem){
var highlight = {};
$.each(newresult.highlighting, function(i, hitem){
var match = hitem.description[0].match(/<em>(.*?)<\/em>/);
highlight[i] = match[1];
});
$.each(newresult.response.docs, function(i, item){
var word = highlight[item["UID_PK"]];
var result = item.description.replace(new RegExp(word, 'g'), '<em>' + word + '</em>');
// do something with the result
});
item.description.replace(word, '<em>' + word + '</em>');