1、json.load/json.loads和json.dump/json.dumps

Python中用json.load() json.loads()加载json数据的方法_ScratKong的博客-CSDN博客_json.loads()方法

①json.dumps()用于将dict类型的数据转成str,因为如果直接将dict类型的数据写入json文件中会发生报错,因此在将数据写入时需要用到该函数。

② json.loads()用于将str类型的数据转成dict。

③ json.dump()用于将dict类型的数据转成str,并写入到json文件中。

④ json.load()用于从json文件中读取数据。

2、isinstance函数

json python 读取 python读取json并解析_数据

json python 读取 python读取json并解析_javascript_02

json python 读取 python读取json并解析_javascript_03

3、# -*-coding:utf-8-*- 

4、练习1:

json python 读取 python读取json并解析_json python 读取_04

json python 读取 python读取json并解析_java_05

import json

filename = (r'C:\Users\Administrator\Downloads\parse_json.json')

with open(filename,'r',encoding='utf-8') as f:

    jsObj = json.load(f)

urlTotol = []

for one in jsObj['item']:

    url1 = one["request"]['url']

    print(url1)

    if url1.get('raw'):#判断key为空

        urlTotol.append(url1['raw'])

    else:

        if url1.get('host'):

            host ='.'.join(url1['host'])#字符串的拼接

        if url1.get('path'):

            if isinstance(url1.get('path'), list):#path为字符串、list进行处理

                path = '/'.join(url1['path'])

                url = host + path

            elif isinstance(url1.get('path'), str):

                url = host + url1['path']

        if url1.get('query'):

            query1 = []

            for one in url1['query']:

                query1.append(one['key']+'='+one['value'])

            query = '?'.join(query1)

        urlTotol.append(url+query)

print('urlTotol>>>>',urlTotol)

import json

filename = (r'C:\Users\Administrator\Downloads\parse_json.json')

with open(filename,'r',encoding='utf-8') as f:

    jsObj = json.load(f)

urlTotol = []

for one in jsObj['item']:

    url1 = one["request"]['url']

    print(url1)

    if url1.get('raw'):

        urlTotol.append(url1['raw'])

    else:

        if url1.get('host'):

            host ='.'.join(url1['host'])

            print('host>>>',host)

        if url1.get('path'):

            if isinstance(url1.get('path'), list):

                path = '/'.join(url1['path'])

                url = host +'/'+ path

                print('url>>>',url)

            elif isinstance(url1.get('path'), str):

                url = host + url1['path']

                print('url>>>', url)

        if url1.get('query'):

            query1 = []

            for one in url1['query']:

                query1.append(one['key']+'='+one['value'])

            query = '&'.join(query1)

            print('query>>>',query)

        urlTotol.append(url+'?'+query)

print('urlTotol>>>>',urlTotol)