zoukankan      html  css  js  c++  java
  • str转json | python的list、dict转json string | json读写 | jsonpath操作

      str转json:

    import json
    
    #json字符串,js类型跟字符串有关系,平时最多是字典
    mydict='{"name":"yincheng","QQ":["758564524","1234"]}'  #dict
    mydict='[1,2,3,4,5,6]'  #list
    print( json.loads(mydict) )
    print( type( json.loads(mydict) ) )

      python的list、dict转json string

    import json
    import chardet
    
    #json字符串,json类型根字符串有关系,平时最多是字典
    mydict={"name":"yincheng","QQ":["77025077","12345"]}
    mydict=[1,2,3,4,5,6]
    print( json.dumps(mydict) )
    print( type( json.dumps(mydict) ) )
    #查看编码
    print( chardet.detect( json.dumps(mydict) ) )

      json读写:

    import json
    ''' 
    #写入  dump
    listStr = [{"city": "北京"}, {"name": "⼤刘"}]
    json.dump(listStr, open("listStr.json","w"), ensure_ascii=False)
    dictStr = {"city": "北京", "name": "⼤刘"}
    json.dump(dictStr, open("dictStr.json","w"), ensure_ascii=False)
    '''
    
    #读取  load
    strList = json.load(open("listStr.json"))
    print(strList)
    # [{u'city': u'u5317u4eac'}, {u'name': u'u5927u5218'}]
    strDict = json.load(open("dictStr.json"))
    print(strDict)
    for  data in strDict:
        print(data,strDict[data])

      jsonpath操作:

    import urllib
    import urllib.request
    import jsonpath
    import json
    import chardet
    #jsonpath
    # $ 根节点  ;  @ 现行节点  ;  . or [] 取子节点 ;  .. 不管位置,选择符合条件的条件
    # * 匹配所有元素节点  ;[] 迭代器标识(可在里边做简单的操作,如数组下标,根据内容选值等);  ?() 支持过滤操作
    url="https://www.lagou.com/lbs/getAllCitySearchLabels.json"
    jsonstr=urllib.request.urlopen(url).read().decode('utf-8') #抓取网页的json数据
    jsontree=json.loads(jsonstr)  #转化为json对象
    #mylist=jsonpath.jsonpath(jsontree,"$..name")   #$根节点 ;  $..name 包含name的节点
    #mylist=jsonpath.jsonpath(jsontree,"$..id") #$根节点 ;  $..id 包含id的节点
    #mylist=jsonpath.jsonpath(jsontree,"$..code") #$根节点 ;  $..code 包含code的节点
    mylist=jsonpath.jsonpath(jsontree,"$.content") #第一个节点
    mylist=jsonpath.jsonpath(jsontree,"$.content.data") #第二个节点
    mylist= jsonpath.jsonpath(jsontree,"$.content.data.allCitySearchLabels") #第三个节点
    mylist= jsonpath.jsonpath(jsontree,"$..allCitySearchLabels") #第三个节点
    mylist= jsonpath.jsonpath(jsontree,"$.content.data[]") #第二个节点的孩子节点
    mylist= jsonpath.jsonpath(jsontree,"$*name")  #判断所有节点是否包含name
    mylist= jsonpath.jsonpath(jsontree,"$.content..allCitySearchLabels.*") #allCitySearchLabel后续所有节点
    for line in mylist:
        print(line)
        for key in line:
            print(key,line[key])
    
    mylist= jsonpath.jsonpath(jsontree,"$.content.data.allCitySearchLabels")
    for line in mylist:
        print(line)
        for key  in line:
            print(key,line[key])
    
    mylist= jsonpath.jsonpath(jsontree,"$.content.data.allCitySearchLabels")
    for line in mylist:
        print(line)
        for key  in line:
            print(key,line[key])
            for  data  in line[key]:
                print(data)
  • 相关阅读:
    异步文档树解决方案
    兼容IE低版本的文件上传解决方案
    CentOS-常用命令(版本:7.x)
    搭建Nexus3私服(含使用说明,支持CentOS、Windows)
    CentOS-搭建MinIO集群
    GitLab升级(yum安装版v11.11.8~12.0.12)
    yum安装GitLab-v11.11.8(git私服)
    Docker中容器的备份和恢复(可迁移)
    Nexus3配置yum私服
    局域网连接数据慢的方法汇总
  • 原文地址:https://www.cnblogs.com/my-global/p/12484156.html
Copyright © 2011-2022 走看看