如何搜索返回的json结果

时间:2010-08-16 09:14:49

标签: jquery asp.net-mvc-2

我试图找回去搜索返回的json结果。返回的结果如:

{"Result":["Css","java","jquery","asp.net","mvc","javascript","asp","c#"]}

我希望在数组或其他json对象中获得以“j”开头的所有单词。

2 个答案:

答案 0 :(得分:3)

不幸的是,没有searchJSONandReturnAllEntrysStartingWith('J')方法。但你可以自己轻松完成。

function searchJSONandReturnAllEntrysStartingWith(ch){
        var jsonres = window.JSON.parse('{"Result":["Css","java","jquery","asp.net","mvc","javascript","asp","c#"]}'),
        filter  = [];

    filter = $.map(jsonres.Result, function(elem, i){
        if(elem.charAt(0) === ch) return elem;
    });

    return filter;
}

alert(searchJSONandReturnAllEntrysStartingWith('j'));

答案 1 :(得分:0)

var jsonres = window.JSON.parse('{"Result":["Css","java","jquery","asp.net","mvc","javascript","asp","c#"]}'),
        filter  = [];

    filter = $.map(jsonres.Result, function(elem, i){
                        var wordLength = $("#textbox1").val().length;
                        var beforeEnd = wordLength - 1;
                        var ch = $("#textbox1").val().substring(beforeEnd,wordLength);
        if(elem.charAt(beforeEnd) === ch) return elem;
    });

    return filter;

如果您有更好的解决方案,我会修改它,请告诉我。