python通过对字典的排序,对json字段进行排序的实例
2020-03-11 17:22
681 查看
如下所示:
dic = dict() dic['a'] = 1 dic['b'] = 2 dic['c'] = 3 print(dic.items()) import json jsons = json.dumps(dic) print(jsons)
结果:
dic is: dict_items([('c', 3), ('b', 2), ('a', 1)])
jsons: {"c": 3, "b": 2, "a": 1}
通过使用collecions,进行排序。collections是一个python的内建模块。
import collections dic = collections.OrderedDict() # dic = dict() dic['a'] = 1 dic['b'] = 2 dic['c'] = 3 print("dic is:",dic.items()) import json jsons = json.dumps(dic) print("jsons:",jsons)
结果:
dic is: odict_items([('a', 1), ('b', 2), ('c', 3)])
jsons: {"a": 1, "b": 2, "c": 3}
补充拓展:对JSON集合 某个键进行升序/降序排列
我就废话不多说了,直接上代码吧
$(document).ready(function () { //对json进行降序排序函数 var colId="age" var desc = function(x,y) { return (x[colId] < y[colId]) ? 1 : -1 } //对json进行升序排序函数 var asc = function(x,y) { return (x[colId] > y[colId]) ? 1 : -1 } var arr2 = [ {name:"kitty", age:12}, {name:"sonny", age:9}, {name:"jake", age:13}, {name:"fun", age:24} ]; document.writeln("按age进行升序排序:<br>"); arr2.sort(asc); //升序排序 document.writeln(JSON.stringify(arr2)); document.writeln("<br>按age进行降序排序:<br>"); arr2.sort(desc); //降序排序 document.writeln(JSON.stringify(arr2)); });
以上这篇python通过对字典的排序,对json字段进行排序的实例就是小编分享给大家的全部内容了,希望能给大家一个参考
您可能感兴趣的文章:
相关文章推荐
- python通过对字典的排序,对json字段进行排序
- Python数据分析中Groupby用法之通过字典或Series进行分组的实例
- Python实现针对json中某个关键字段进行排序操作示例
- python对字典进行排序实例
- Python 针对Json中某个关键字段进行排序
- python对字典进行排序实例
- Python读取json文件,并转化为字典进行提取字段(出现索引must be int,not str)解决方案
- 005_004 Python 统计值 并按照次数排序 可以通过字典的值对key进行排序
- Python: 字典列表: itemgetter 函数: 根据某个或某几个字典字段来排序列表
- Python: 字典列表: itemgetter 函数: 根据某个或某几个字典字段来排序列表
- python字典多条件排序方法实例
- 对python中的字典进行排序
- python学习——采用sorted方法对字典的键、值进行排序
- python 统计数组中元素出现次数并进行排序的实例
- [python]对字典进行排序
- JS中排序返回的JSON字符串(做peoplesoft项目无法对某一个字段进行排序后返回)
- javascript 按字段对json进行排序
- 对jsonarray中某个对象的某一个字段进行排序
- Python 通过某个关键字排序字典列表
- python基础(如何根据字典中值的大小,对字典的项进行排序)