golang python java 对比 转json格式字符串 默认加空格

 小结:

1、go排序

python 默认不排序,排序需显示指明

 

 

import json

d = {
"c": 12,
"a": {
"b1": 123,
"a1": 345,
"c1": {
"b11": 34,
"a11": 23
}
},
"b": 12
}
print(json.dumps(d, sort_keys=False))
print(json.dumps(d, sort_keys=True))

{"c": 12, "a": {"b1": 123, "a1": 345, "c1": {"b11": 34, "a11": 23}}, "b": 12}
{"a": {"a1": 345, "b1": 123, "c1": {"a11": 23, "b11": 34}}, "b": 12, "c": 12}



    d := map[string]interface{}{
        "c": 12,
        "a": map[string]interface{}{
            "b1": 123,
            "a1": 345,
            "c1": map[string]interface{}{
                "b11": 34,
                "a11": 23,
            },
        },
        "b": 12,
    }
    s1, _ := json.Marshal(d)
    log.Printf(string(s1))
{"a":{"a1":345,"b1":123,"c1":{"a11":23,"b11":34}},"b":12,"c":12}


package org.example;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson2.*;

//TIP 要<b>运行</b>代码,请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Main {
static void main() {
Map m2 = new HashMap();
m2.put("a1", 11);
m2.put("c1", 33);
m2.put("b1", "22");

Map m1 = new HashMap();
m1.put("a", 1);
m1.put("c", 3);
m1.put("f", 2);
m1.put("b", m2);

System.out.println(m1);
// https://alibaba.github.io/fastjson2/
String jsonString = JSON.toJSONString(m1);

System.out.println(jsonString);

String jsonString1 = JSONObject.toJSONString(m1, JSONWriter.Feature.SortMapEntriesByKeys);

System.out.println(jsonString1);
}
}
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.64</version>
<scope>compile</scope>
</dependency>

{a=1, b={a1=11, c1=33, b1=22}, c=3, f=2}
WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::arrayBaseOffset has been called by com.alibaba.fastjson2.util.JDKUtils (file:/C:/Users/FXL/.m2/repository/com/alibaba/fastjson2/fastjson2/2.0.64/fastjson2-2.0.64.jar)
WARNING: Please consider reporting this to the maintainers of class com.alibaba.fastjson2.util.JDKUtils
WARNING: sun.misc.Unsafe::arrayBaseOffset will be removed in a future release
{"a":1,"b":{"a1":11,"c1":33,"b1":"22"},"c":3,"f":2}
{"a":1,"b":{"a1":11,"b1":"22","c1":33},"c":3,"f":2}


com/alibaba/fastjson2/JSONWriter.java
// https://alibaba.github.io/fastjson2/
/**
* The serialized Map will first be sorted according to Key,
* and is used in some scenarios where serialized content needs to be signed.
* SortedMap and derived classes do not need to do this.
*
* @since 2.0.48
*/
SortMapEntriesByKeys(1L << 41),



 

   jsonString, _ := json.Marshal(v)
 

\Go\src\encoding\json\encode.go

 

// Map values encode as JSON objects. The map's key type must either be a
// string, an integer type, or implement [encoding.TextMarshaler]. The map keys
// are sorted and used as JSON object keys by applying the following rules,
// subject to the UTF-8 coercion described for string values above:
//   - keys of any string type are used directly
//   - [encoding.TextMarshalers] are marshaled
//   - integer keys are converted to strings
 
 

 

 
 json.dumps
If *sort_keys* is true (default: ``False``), then the output of
dictionaries will be sorted by key.


If specified, ``separators`` should be an ``(item_separator,
key_separator)`` tuple. The default is ``(', ', ': ')`` if *indent* is
``None`` and ``(',', ': ')`` otherwise. To get the most compact JSON
representation, you should specify ``(',', ':')`` to eliminate
whitespace.


 
import json

s = '{"a":1,"b":{"a1":11,"b1":"22","c1":33,"c3":"中文"},"c":3,"f":2}'
d = {
"a": 1,
"b": {
"a1": 11,
"c3": "中文",
"c1": 33,
"b1": "22"
},
"c": 3,
"f": 2
}
a = json.dumps(d, sort_keys=True, ensure_ascii=False, separators=(',', ':'))
b = json.dumps(d, sort_keys=True, ensure_ascii=False, )

print(a == b, a == s, b == s)
 

{"a":1,"b":{"a1":11,"b1":"22","c1":33,"c3":"中文"},"c":3,"f":2}
{"a": 1, "b": {"a1": 11, "b1": "22", "c1": 33, "c3": "中文"}, "c": 3, "f": 2}
False True False

 
 
 
 
posted @ 2024-07-23 14:36  papering  阅读(32)  评论(0)    收藏  举报