html tool

显示标签为“json”的博文。显示所有博文
显示标签为“json”的博文。显示所有博文

2024年2月18日星期日

转:python对json的读取,load()与loads()的区别

 参考:https://blog.csdn.net/m0_51971452/article/details/111701927

原文:

json.dumps()把数据类型转换成字符串 

json.dump()把数据类型转换成字符串并存储在文件中 

json.loads()把字符串转换成数据类型 

json.load()把文件打开从字符串转换成数据类型

例子:

读取load

     f = open("testdata/%s" % s) #这里测试文件为json格式化后的格式保存

    cur_test = json.load(f) #这里json文件默认使用了2个空格为分隔符,不指定可以被识别

    f.close()

   print(cur_test)
   print(type(cur_test))

写入dumps: 
--字典数据用dumps()编码成json字符串,存储到json文件中
with open("temp/%s.json" % s,"w") as f:
    f.write(json.dumps(load_dict, indent=4, ensure_ascii=False))
--用dump()方法将字典数据写入json文件中
with open("temp/%s.json" % s,"w") as f:
    json.dump(load_dict, write_f, indent=4, ensure_ascii=False)

2023年11月30日星期四

jq获得全部的json的keys

 参考:https://alingse.github.io/jq-manual-cn/manual/v1.5/#TypesandValues

处理方式:

cat *.json|jq "path(..)" 

输出即全部keys,类似如下:



原文:

Note that the path expressions are not different from normal expressions. The expression path(..|select(type=="boolean")) outputs all the paths to boolean values in ., and only those paths.

examples:

  • program: ‘path(.a[0].b)’ input: ‘null’ output: [’[“a”,0,”b”]’]
  • program: ‘[path(..)]’ input: ‘{“a”:[{“b”:1}]}’ output: [’[[],[“a”],[“a”,0],[“a”,0,”b”]]’]

2023年9月15日星期五

python中json 存文件

 参考:https://blog.csdn.net/leviopku/article/details/103773219

     r = json.dumps(report_json)

     f = open(sf, "w")

     f.write(r)

     f.close()




2023年9月13日星期三

转:python中json中单引号引起的TypeError: expected string or buffer 解决

 参考:https://www.cnblogs.com/pinpin/p/10619471.html

import json

data = [{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}]
print(type(data),data)

执行结果:

<class 'list'> [{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}]

但是了,json是不支持单引号的。可以用下面的方法转换

json_string=json.dumps(s)

python_obj=json.loads(json_string)

2022年10月27日星期四

转:python将字典保存成json 文件

 https://blog.csdn.net/leviopku/article/details/103773219

python将字典保存成json

import json

a = {

    "name": "dabao",

    "id":123,

    "hobby": {

        "sport": "basketball",

        "book": "python study"

    }

}

b = json.dumps(a)

f2 = open('new_json.json', 'w')

f2.write(b)

f2.close()

首先通过json.dumps()把dict降级为字符串。再将字符串写入json文件中。就是这么简单


2021年4月7日星期三

转: java.lang.NoSuchMethodError: No such DSL method 'readJSON'

 https://stackoverflow.com/questions/46841877/java-lang-nosuchmethoderror-no-such-dsl-method-readjson


Using Pipeline Utility Steps Plugin you can use the readJSON function.

def props = readJSON text: '{ "key": "value" }'