import jsonpath
import json
1、字典转json,使用json.dumps(json)
2、字典转换成json字符串,json.loads(dict)
简单案例:
str1 = ‘{“code”:0,“data”:{“startRow”:1,“navigatepageNums”:[1],“lastPage”:1,“prePage”:0,“hasNextPage”:false,“nextPage”:0,“pageSize”:20,“endRow”:2,“list”:[{“shopCost”:6.0000,“replayInventoryNum”:-9.0,“categoryName”:“未分类”,“spec”:"",“replayIsSubmit”:false,“replayProfNum”:0.0,“barcode”:“2511710123405”,“brand”:“无品牌”,“profNum”:9.0,“stocktakingNum”:0.0,“commodityId”:3199622,“isSubmit”:false,“personNum”:0,“parentId”:0,“replayPersonNum”:1,“inventoryNum”:-9.0,“unit”:"",“commodityCost”:6.0000,“commodityName”:“香菜”},{“shopCost”:10.0000,“replayInventoryNum”:-25.0,“categoryName”:“未分类”,“spec”:“200g”,“replayIsSubmit”:false,“replayProfNum”:0.0,“barcode”:“2511706847371”,“brand”:“无品牌”,“profNum”:25.0,“stocktakingNum”:0.0,“commodityId”:3199623,“isSubmit”:false,“personNum”:0,“parentId”:0,“replayPersonNum”:1,“inventoryNum”:-25.0,“unit”:“个”,“commodityCost”:10.0000,“commodityName”:“芹菜”}],“pageNum”:1,“navigatePages”:8,“total”:2,“navigateFirstPage”:1,“pages”:1,“size”:2,“firstPage”:1,“isLastPage”:true,“hasPreviousPage”:false,“navigateLastPage”:1,“isFirstPage”:true},“message”:“操作成功!”}’
dict1 = json.loads(str1)
commodityId = jsonpath.jsonpath(dict1, “$…commodityId”)
print(commodityId)