python格式化json,python解析json格式数据
必须编写python脚本,在调试时打印json格式的消息。 直接打印也不知道层次结构。 可以使用json.dumps进行格式打印
导入JSON
导入请求
eftest_JSON(:
打印(r.JSON ) )
print(json.dumps(r.JSON (,indent=2,ensure_ascii=False ) ) r.JSON ) )是JSON对象,indent是缩进,indent是
格式打印前:
格式打印前:
格式化打印后:
json.dumps方法的源代码:
defdumps(obj,*,skipkeys=False,ensure_ascii=True,check_circular=True,
allow_nan=True,cls=None,indent=None,separators=None,
default=None,sort_keys=False,qsdlkw ) :
' ' serialize ` ` obj ` ` toajsonformatted ` ` str ` `。
if ` ` skip keys ` ` is true then ` ` dict ` ` keysthatarenotbasictypes
` ` str `,` int `,` float `,` bool `,` None ` ` ) will be skipped
insteadofraisinga ` ` FDD JB/pif ` ` ensure _ ascii ` ` is false,thenthereturnvaluecancontainnnon-ascii
charactersiftheyappearinstringscontainedin ` ` obj ` `.otherwise,all
schcharactersareescapedinjsonstrings。
If ``check_circular`` is false,then the circular reference check
forcontainertypeswillbeskippedandacircularreferencewill
If ``allow_nan`` is false,then it will be a ``ValueError`` to
序列化过程` `浮动` ` values ` ` nan ` `、` inf `、`-inf ` ` ) in
strictcomplianceofthejsonspecification,instead of using the
JavaScript equivalents (``NaN ` `,` Infinity ` `,`-Infinity ` ` )。
if ` ` indent ` ` is anon-negative integer,then JSON array elements and
objectmemberswillbepretty-printedwiththatindentlevel.an indent
level of0willonlyinsertnewlines.` ` none ` ` isthemostcompact
If specified,` ` separators ` ` should bean ``` (item _ separator,key_separator ) ` ` `
tuple. The default is ` ` ('、'、'、'、' : ' ) ` ` if *indent* is ``None`` and '、' : ' ) ` otherwise.togethe
you should specify ` ` ('、'、'、' : ' ) ` ` to eliminate whitespace。
` `默认(obj ) ` isafunctionthatshouldreturnaserializableversion
ofobjorraisetypeerror.thedefaultsimplyraisestypeerror。
if * sort _ keys * is true (default : ` ` false ` ` ),then the output of
dictionaries will be sorted by key。
touseacustom ` ` JSON编码器` ` subclass (e.g.onethatoverridesthe
` `.default (` ` methodtoserializeadditionaltypes )、specify it with
the ``cls`` kwarg; otherwise ` ` JSON编码器` ` is used。
''''
# cached编码器
if (notskipkeysandensure _ ascii and
check_circular and allow_nan and
clsisnoneandindentisnoneandseparatorsisnoneand
defaultisnoneandnotsort _ keysandnotkw ) :
if cls is None:
cls=JSON编码器
skipkeys=skipkeys,ensure_ascii=ensure_ascii,
check_circular=check_circular,allow_nan=allow_nan,indent=indent,
separators=separators,default=default,sort_keys=sort_keys,
qsdlkw ).encode(obj ) )。
希望以上是正文的全部内容,对大家的学习有帮助。 另外,我希望你支持很多编剧。