我如何排序我的json结果,因为我的json结果格式而感到困惑

时间:2015-10-08 05:46:40

标签: jquery json asp.net-mvc-4 sorting

我的JSON结果是

`"{  
   "24":"Andaman and Nicobar Islands",
   "31":"Andhra Pradesh",
   "8":"Arunachal Pradesh",
   "32":"Assam",
   "33":"Bihar",
   "28":"Chandigarh",
   "30":"Chhattisgarh",
   "20":"Dadra and Nagar Haveli",
   "27":"Daman and Diu",
   "36":"Delhi",
   "39":"Goa",
   "23":"Gujarat",
   "18":"Haryana",
   "38":"Himachal Pradesh",
   "1":"Jammu & Kashmir",
   "5":"Jammu and Kashmir",
   "21":"Jharkhand",
   "35":"Karnataka",
   "7":"Kerala",
   "3":"Lakshadweep",
   "40":"Madhya Pradesh",
   "4":"Maharashtra",
   "26":"Manipur",
   "16":"Meghalaya",
   "37":"Mizoram",
   "14":"Nagaland",
   "29":"National Capital Region (India)",
   "19":"Odisha",
   "2":"Orissa",
   "34":"Puducherry",
   "6":"Punjab",
   "12":"Rajasthan",
   "15":"Sikkim",
   "25":"Tamil Nadu",
   "17":"TamilNadu",
   "9":"Tripura",
   "22":"Uttar Pradesh",
   "10":"Uttarakhand",
   "11":"West Bengal"
}"`

我发现很多关于json排序的文章,但它们有不同的格式我认为这就是为什么它不起作用我试过跟随 url_1

url_2

url_3

url_4

我是JSON和jquery的新手,请好心。

2 个答案:

答案 0 :(得分:0)

您可以将其转换为数组并根据需要对其进行排序。它就像



var myObject = {"24":"Andaman and Nicobar Islands","31":"Andhra Pradesh","8":"Arunachal Pradesh","32":"Assam","33":"Bihar","28":"Chandigarh","30":"Chhattisgarh","20":"Dadra and Nagar Haveli","27":"Daman and Diu","36":"Delhi","39":"Goa","23":"Gujarat","18":"Haryana","38":"Himachal Pradesh","1":"Jammu & Kashmir","5":"Jammu and Kashmir","21":"Jharkhand","35":"Karnataka","7":"Kerala","3":"Lakshadweep","40":"Madhya Pradesh","4":"Maharashtra","26":"Manipur","16":"Meghalaya","37":"Mizoram","14":"Nagaland","29":"National Capital Region (India)","19":"Odisha","2":"Orissa","34":"Puducherry","6":"Punjab","12":"Rajasthan","15":"Sikkim","25":"Tamil Nadu","17":"TamilNadu","9":"Tripura","22":"Uttar Pradesh","10":"Uttarakhand","11":"West Bengal"};

var myArray=[];
for(var index in myObject){
myArray.push(myObject[index]);
}
document.write(myArray.sort());




答案 1 :(得分:0)

如果需要,只需解析数据。这就是全部。不需要排序。请参阅以下演示的输出:



var json = {
        "24":"Andaman and Nicobar Islands",
        "31":"Andhra Pradesh",
        "8":"Arunachal Pradesh","32":"Assam","33":"Bihar",
        "28":"Chandigarh",
        "30":"Chhattisgarh",
        "20":"Dadra and Nagar Haveli",
        "27":"Daman and Diu",
        "36":"Delhi",
        "39":"Goa",
        "23":"Gujarat",
        "18":"Haryana",
        "38":"Himachal Pradesh",
        "1":"Jammu & Kashmir",
        "5":"Jammu and Kashmir",
        "21":"Jharkhand",
        "35":"Karnataka",
        "7":"Kerala",
        "3":"Lakshadweep",
        "40":"Madhya Pradesh",
        "4":"Maharashtra",
        "26":"Manipur",
        "16":"Meghalaya",
        "37":"Mizoram",
        "14":"Nagaland",
        "29":"National Capital Region (India)",
        "19":"Odisha",
        "2":"Orissa",
        "34":"Puducherry",
        "6":"Punjab",
        "12":"Rajasthan",
        "15":"Sikkim",
        "25":"Tamil Nadu",
        "17":"TamilNadu",
        "9":"Tripura",
        "22":"Uttar Pradesh",
        "10":"Uttarakhand",
        "11":"West Bengal"
    };

console.log( JSON.stringify(json) );
//OUTPUT: {"1":"Jammu & Kashmir","2":"Orissa","3":"Lakshadweep","4":"Mah.......