2012-07-17 35 views
0
{ 
    "JsonResult": { 
     "List": [{ 
      "Subject": "Something", 
      "Type": 0, 
      "TypeDescription": "Referral" 
     }], 
    } 
} 

這是我的樣本JSON響應,同時擊中我的服務,在那之後我有一個按鈕,它攜帶像Subject, Type and TypeDescription選項我得到。排序我的JSON響應

我如何可以排序基於我送參數的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響應,我需要根據我送參數在運行時進行排序。

+1

看起來這應該是'jsonResponse.JsonResult.List.sort(...)' – Matt 2012-07-17 09:10:52

+0

http://stackoverflow.com/questions/979256/how-to-sort-一個陣列-的JavaScript的對象 – 2012-07-17 11:00:17

回答

0

這裏是一個js搗鼓你這將使你更深入的瞭解。

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> 
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); 
     }); 
} 

'paramater',第二個參數是表示要查找的鍵的字符串,而不是直接的字段存取器語句。因此,它不能從使用點運算符的JSON進行訪問。

同時,應注意:它應該是:

jsonResponse.JsonResult.List.sort 

前陣子,我想要做的一樣:看到這裏的一些技巧:
#1:Sorting array of custom objects in JavaScript