我的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
我是JSON和jquery的新手,请好心。
答案 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.......