zoukankan      html  css  js  c++  java
  • 输入一个json输入一个jsonpath

    1,最近在公司里因为业务需求需要写一个函数实现,输入一个json输出一个jsonpath

    以下是代码:

    
    
    def data_handle(data, jsonpath):  # 第一次调用这个函数的时候,jsonpath 设为 $. 或$
    if isinstance(data, dict): #判断数据类型,如果是字典类型执行这个
    for key in data:
    if isinstance(data[key], str) or isinstance(data[key], int):
    jsonpath1 = jsonpath + key
    if key != "jsonpath":
    print(jsonpath1) # 这是本次调用该函数可以直接计算出来jsonpath。 这个地方只输出,也可以写到一起txt中

    elif isinstance(data[key], dict):
    jsonpath1 = jsonpath + key + '.'
    data_handle(data[key],jsonpath1)
    # dict1['jsonpath'] = jsonpath1
    # templist.append(dict1)
    # print(templist)
    elif isinstance(data[key], list):
    jsonpath1 = jsonpath + key
    data_handle(data[key], jsonpath1)
    elif isinstance(data, list): # 第一次jsonpath为 $
    for each in data:
    if isinstance(each, dict):
    jsonpath1 = jsonpath + '[' + str(data.index(each)) + ']' + '.'
    dic2 = {}
    dic2 = each
    dic2['jsonpath'] = jsonpath1
    templist.append(dic2)
    elif isinstance(each, list):
    jsonpath1 = jsonpath + "[" + str(data.index(each)) + "]"
    data_handle(each, jsonpath1)
    elif isinstance(each, str) or isinstance(each, int):
    jsonpath1 = jsonpath + '[' + str(data.index(each)) + ']'
    if each != "jsonpath":
    print(jsonpath1)
    data_handle(ex_data,"$.")#调用函数传入一个data和一个jsonpath的初始值即可
  • 相关阅读:
    poj 1562 Oil Deposits
    poj 1650 Integer Approximation
    snmp4j 编程
    ubuntu 13.04 163源(亲测可用)
    c语言中static 用法总结(转)
    Spring入门
    Hibernate入门
    Struts2入门教程
    素数距离问题
    ASCII码排序
  • 原文地址:https://www.cnblogs.com/dashouzhang/p/7305321.html
Copyright © 2011-2022 走看看