0
{
    "JsonResult": {
        "List": [{
            "Subject": "Something",
            "Type": 0,
            "TypeDescription": "Referral"
        }],
     }
}

这是我在点击服务时得到的示例 json 响应,之后我有一个带有选项的按钮,例如Subject, Type and TypeDescription.

如何根据我发送的参数对 Json 响应进行排序。

function sortItems(jsonResponse,paramater){
          var sorted =  jsonResponse.List.sort( function(a, b) {
          var nameA = a.paramater.toLowerCase(),
          nameB = b.paramater.toLowerCase();
          return nameA.localeCompare(nameB);
}

这是我正在使用的排序功能,但它不起作用。我已经有了 JSON 响应,我需要在运行时根据我发送的参数进行排序。

4

2 回答 2

1
function sortItems(jsonResponse, paramater) {
      var sorted =  jsonResponse.JsonResult.List.sort(function(a, b) {
          var nameA = a[paramater].toLowerCase(), //take note, []
          nameB = b[paramater].toLowerCase(); //same here
          return nameA.localeCompare(nameB);
      });
}

第二个参数“参数”是一个字符串,表示要查找的键,而不是直接的字段访问器语句。因此无法使用点运算符从 json 访问它。

另外,请注意:它应该是:

jsonResponse.JsonResult.List.sort

不久前,我也想做同样的事情:请参阅此处了解一些技巧:
Stackoverflow: Sorting array of custom objects in JavaScript

于 2012-07-17T09:11:26.403 回答
0

这是给你的一个 Js Fiddle,它会给你一个更深入的洞察力。

http://jsfiddle.net/spechackers/EdAWL/

<script type="text/javascript">
        onerror = function (a, b, c) {
            alert([a, b, c]);
        };
    </script>
<script type="text/javascript">
    var x = {
        "JsonResult": {
            "List": [{
                "Subject": "My book report on J. K. Rowling's <u>Harry Potter</u> series.",
                "Date": "Jan 25th 2009",
                "Type": "Book Report",
                "Class": "English"
            }, {
                "Subject": "My book report on Charles Dickens' <u>Oliver Twist</u> novel.",
                "Date": "May 1st 2003",
                "Type": "Book Report",
                "Class": "English"
            }, {
                "Subject": "My book report on J. R. R. Tolkien's <u>The Lord of the Rings</u> series.",
                "Date": "Aug 7th 2007",
                "Type": "Book Report",
                "Class": "English"
            }, {
                "Subject": "The civil war in a nutshell.",
                "Date": "Feb 26th 2009",
                "Type": "Essay",
                "Class": "History"
            }, {
                "Subject": "How does the republican party manage if we life in a democracy?",
                "Date": "Apr 5th 2010",
                "Type": "Essay",
                "Class": "Government"
            }, {
                "Subject": "A bogus entry",
                "Date": "Jan 1st 2000",
                "Type": "Bogus",
                "Class": "None"
            }, {
                "Subject": "Zombie followers don't prove anything.",
                "Date": "Nov 2nd 2004",
                "Type": "Essay",
                "Class": "Religion"
            }]
        }
    };
</script>
<script type="text/javascript">
    if (typeof Object.prototype.toSource == 'undefined') {
        Object.prototype.toSource = function () {
            return (typeof JSON != 'undefined' && typeof JSON.stringify == 'function') ? JSON.stringify(this) : this.toString();
        };
    }

    function sortItems(jsonResponse, paramater) {
        var sorted = jsonResponse.List.sort(

        function (a, b) {
            var nameA = a[paramater].toLowerCase();
            var nameB = b[paramater].toLowerCase();
            return nameA.localeCompare(nameB);
        });
        return sorted;
    }

    function makeRow(ar) {
        return "<tr><td>" + ar[0] + "</td><td>" + ar[1] + "</td><td>" + ar[2] + "</td><td>" + ar[3] + "</td></tr>";

    }
</script>
<script type="text/javascript">
    var mySortedList = sortItems(x.JsonResult, "Subject");
    //alert(mySortedList.toSource());
    var outTable = "<table>";
    outTable += "<tr><th>Subject</th><th>Date</th><th>Type</th><th>Class</th></tr>";
    for (var i = 0; i < mySortedList.length; i++) {
        outTable += makeRow([mySortedList[i].Subject, mySortedList[i].Date, mySortedList[i].Type, mySortedList[i].Class]);
    }
    outTable += "</table>";
    document.write(outTable);
</script>
于 2012-07-17T10:50:03.687 回答