JSON sorting in Python

Source: Internet
Author: User

Write a question about JSON sorting:

Convert the resulting data to the JSON format to the AJAX, you will find the foreground data is very likely and we are in the background of the content ' not quite the same ', if not unexpectedly, JSON has been self-sorting processing, as to what format I have not studied, of course, it should not be alphabetical

So what do we do if we want to sort alphabetically or numerically in the foreground?

If it is an array-type JSON object, sorting can use Array.Sort,

var people = [{name: ' A75 ', item1:false},{name: ' Z32 ', item1:true},{name: ' E77 ', item1:false}];

function Sortbykey (array, key) {
Return Array.Sort (function (A, b) {
var x = A[key]; var y = B[key];
Return ((x < y)?-1: ((x > Y)? 1:0));
});
}

People = Sortbykey (People, ' name ');
The results are a75,e77,z32 in this order.

And if it's not an array type JSON object, write two sets of data compared

data1 ={‘b‘:789,‘c‘:456,‘a‘:123}

data2 = { ‘a‘ : 123 , ‘b‘ : 789 , ‘c‘ : 456 } d1 = json.dumps(data1,sort_keys = True ) d2 = json.dumps(data2) d3 = json.dumps(data2,sort_keys = True ) print d1 print d2 print d3 print d1 = = d2 print d1 = = d3

Output:

{"A": 123, "B": 789, "C": 456}
{"A": 123, "C": 456, "B": 789}
{"A": 123, "B": 789, "C": 456}
False
True

This parameter is used in this sort_keys. Btw

The indent parameter is the indentation meaning, which can make the data storage format more elegant.

data1  = { ‘b‘ : 789 , ‘c‘ : 456 , ‘a‘ : 123 } d1  = json.dumps(data1,sort_keys = True ,indent = 4 ) print d1 

Output:

{
"A": 123,
"B": 789,
"C": 456
}

After the output is formatted, it becomes more readable, but it is populated by adding some redundant blanks. JSON is mainly as a data communication format exists, and network communication is very concerned about the size of the information, useless space will occupy a lot of communication bandwidth, so the appropriate time also to compress the data. The separator parameter can play the role of a tuple that contains a string that splits the object.

print ‘DATA:‘ repr (data) print ‘repr(data)             :‘ len ( repr (data)) print ‘dumps(data)            :‘ len (json.dumps(data)) print ‘dumps(data, indent=2)  :‘ len (json.dumps(data, indent = 4 )) print ‘dumps(data, separators):‘ len (json.dumps(data, separators = ( ‘,‘ , ‘:‘ )))

Output:

DATA: {' A ': 123, ' C ': 456, ' B ': 789}
REPR (data): 30
Dumps (data): 30
Dumps (data, indent=2): 46
Dumps (data, separators): 25

The purpose of compressing the data is achieved by removing the extra whitespace characters, and the effect is quite obvious.

Another useful dumps parameter is Skipkeys, which defaults to false. When the dumps method stores the Dict object, the key must be of type STR, and if there are other types, a TypeError exception will be generated, and if this argument is set to true, it will be more gracefully over.

data  = { ‘b‘ : 789 , ‘c‘ : 456 ,( 1 , 2 ): 123 } print json.dumps(data,skipkeys = True

Output:

{"C": 456, "B": 789}

JSON sorting in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.