我正在尝试使用Python / Django正确解析JSON

时间:2019-07-03 14:56:25

标签: python json python-3.x decode encode

我正在尝试通过请求使用JSON中的某些数据,但是当我尝试进行验证时,总是会遇到解析错误。

我尝试这段代码,从dict转换为字符串,然后转换为json。

import requests
import json

r = requests.get('http://codehere.com', auth=('user', 'pass')).json()
json.loads(json.dumps(r))
print(r)

之前:

{
    "result": [
        "[
            {
                \"IDE_NOMBRE\":\"DUMMY\",
                \"IDE_CODIGO\":1,
                \"TER_IDENTIFICACION\":\"DUMMY\",
                \"TER_CIUEXPCED\":\"\",
                \"TER_DEPEXPCED_NOM\":\"\",
                \"TER_CIUEXPCED_NOM\":\"\",
                \"TER_FCHEXPCED\":null,
                \"TER_SEXO\":\"M\",
                \"TER_APELLIDO1\":\"DUMMY\",
                \"TER_APELLIDO2\":\"DUMMY\u00D1O\",
                \"TER_NOMBRE\":\"DUMMY\",
                \"TER_NOMBRE1\":\"\",
                \"TER_CUINACI\":\"\",
                \"TER_DEPNACI_NOM\":\"\",
                \"TER_CUINACI_NOM\":\"\",
                \"TER_FCHNACI\":null,
                \"TER_NROHIJOS\":0,
                \"ESTCIV_NOMBRE\":\"\",
                \"ESTCIV_CODIGO\":\"\",
                \"NIVESC_CODIGO\":\"\",
                \"NIVESC_NOMBRE\":\"\",
                \"TER_BARRIO\":\"\",
                \"CIU_CODIGO\":\"05001\",
                \"CIU_DEPTO\":\"DUMMY\",
                \"CIU_NOMBRE\":\"DUMMY\",
                \"TER_TELEFONO\":\"\",
                \"TER_EMAIL\":\"\",
                \"TER_DIRECCION\":\"\",
                \"TER_TELMOVIL\":\"\",
                \"TER_PERSONASCARGO\":0,
                \"TIPVIV_DESCRIPCION\":\"\",
                \"CLI_ESTRATO\":\"\",
                \"CLI_FCHAFILIACION\":\"2018-10-01 00:00:00\",
                \"CLI_CODISS\":\"\",
                \"ACT_CODIGO\":\"1\",
                \"ASESOR_WEB\":0,
                \"COD_CIIU\":\"\",
                \"TIPTRIB_CCN\":0,
                \"TIPTRIB_NOMBRE\":\"\",
                \"SECO_CODIGO\":\"\",
                \"SECO_NOMBRE\":\"\",
                \"BAN_CODIGO\":\"DUMMY\",
                \"BAN_NOMBRE\":\"DUMMY\",
                \"TER_TPOCNTA\":\"1\",
                \"TER_CUENTA\":\"DUMMY\",
                \"TER_TIPONATU\":\"N\",
                \"JORLAB_CODIGO\":\"\",
                \"JORLAB_NOMBRE\":\"\",
                \"OCU_CODIGO\":\"\",
                \"OCU_NOMBRE\":\"\",
                \"TPS_CCN\":\"1\",
                \"TPS_DESCRIPCION\":\"NORMAL\",
                \"OBSERVACIONES\":\"\",
                \"TER_TOTING\":0,
                \"TER_TOTEGRE\":0,
                \"TER_TOTACTIV\":0,
                \"TER_TOTPASIV\":0,
                \"TER_TOTPATRIM\":0,
                \"TER_TOTCOST\":0,
                \"CLI_MUJCABFAM\":\"\",
                \"AFECTA_VIVIENDA\":\"\",
                \"TER_ENVIOCORRESP\":\"\",
                \"COENT_CCN\":0,
                \"COENT_NOMBRE\":\"\",
                \"CLI_SALARIO\":0,
                \"CLI_OTRSEGRE\":0,
                \"CTR_CODIGO\":\"3\",
                \"CTR_NOMBRE\":\"DUMMY\",
                \"CLI_ULFCHLIQU\":null,
                \"PRO_CODTER\":0,
                \"PRO_NOMBRE\":\"\",
                \"PRO_NOMBRE_1\":\"DUMMY\",
                \"PRO_CODIGO\":7,
                \"TER_OTRING\":0,
                \"TER_CONOTRING\":\"\",
                \"TER_RELPERPEP\":\"\",
                \"TIPVIV_CCN\":0,
                \"CAR_NOMBRE\":\"\",
                \"NOM_CONY1\":\"\",
                \"NOM_CONY2\":\"\",
                \"APE_CONY1\":\"\",
                \"APE_CONY2\":\"\",
                \"DEP_CODDILI\":\"\",
                \"CIU_CODDILI\":\"\",
                \"TER_FCHDILI\":null,
                \"EMP_CODIGO\":\"E2\",
                \"EMP_NOMBRE\":\"DUMMY\",
                \"TER_EMPLAB\":\"\",
                \"TER_RELLAB\":\"\",
                \"CIU_EMPLAB\":\"\",
                \"CIU_NOMBRE_1\":\"DUMMY\",
                \"TER_DIREMPLAB\":\"\",
                \"TER_TELEMPLAB\":\"\",
                \"TER_CELEMPLAB\":\"\",
                \"TER_SUELDO\":0,
                \"TER_FCHINEMPLAB\":null,
                \"TER_FCHTERCONT\":null,
                \"TER_TIPCONTRA\":\"\",
                \"CTR_NOMBRE_1\":\"\",
                \"NRO_TARVISIO\":\"\",
                \"NRO_TARVISIO1\":\"\",
                \"PAI_PK\":0,
                \"PAI_NAC\":170,
                \"PAIS_CODIGO\":\"\",
                \"TER_AUTTRATDATOS\":\"\",
                \"TER_CCNASESOR\":0,
                \"CIIU_CODIGO_2\":\"\",
                \"CIIU_NOMBRE\":\"\",
                \"SECO_CODIGO_2\":\"\",
                \"SECO_NOMBRE_1\":\"\",
                \"TER_TOTEXCED\":0,
                \"TER_DIGITOVERIF\":\"\",
                \"VALORCOND\":0,
                \"TEL_CONY\":\"\",
                \"EMAIL_CONY\":\"\",
                \"NIVEL_EST_CONY\":\"\",
                \"ACTIVIDAD_CONY\":\"\",
                \"TER_SINANLUCR\":\"\",
                \"TER_SIGLA\":\"\",
                \"TIENSAL_CCODIGO\":0,
                \"TER_SOCIO_S_N\":\"S\",
                \"TER_TELEFONOLAB\":\"\",
                \"CIU_CODIGOLAB\":\"\",
                \"TER_DIRECCIONLAB\":\"\",
                \"COD_CIIU_1\":\"\",
                \"CIIU_NOMBRE_1\":\"\",
                \"TER_TOTCOSTOP\":0,
                \"ESTRATO_CONY\":\"\",
                \"TER_RAZONSOCIAL\":\"\",
                \"CLI_FCHINGRESOCIA\":null,
                \"TER_NOMCOMPLETO\":\"DUMMY DUMMY\u00D1O DUMMY\"
            }
        ]"
    ]
}

结果:

{'result': ['[
        {
            "IDE_NOMBRE": "",
            "IDE_CODIGO": 1,
            "TER_IDENTIFICACION": "",
            "TER_CIUEXPCED": "",
            "TER_DEPEXPCED_NOM": "",
            "TER_CIUEXPCED_NOM": "",
            "TER_FCHEXPCED": null,
            "TER_SEXO": "M",
            "TER_APELLIDO1": "",
            "TER_APELLIDO2": "",
            "TER_NOMBRE": "",
            "TER_NOMBRE1": "",
            "TER_CUINACI": "",
            "TER_DEPNACI_NOM": "",
            "TER_CUINACI_NOM": "",
            "TER_FCHNACI": null,
            "TER_NROHIJOS": 0,
            "ESTCIV_NOMBRE": "",
            "ESTCIV_CODIGO": "",
            "NIVESC_CODIGO": "",
            "NIVESC_NOMBRE": "",
            "TER_BARRIO": "",
            "CIU_CODIGO": "",
            "CIU_DEPTO": "",
            "CIU_NOMBRE": "",
            "TER_TELEFONO": "",
            "TER_EMAIL": "",
            "TER_DIRECCION": "",
            "TER_TELMOVIL": "",
            "TER_PERSONASCARGO": 0,
            "TIPVIV_DESCRIPCION": "",
            "CLI_ESTRATO": "",
            "CLI_FCHAFILIACION": "",
            "CLI_CODISS": "",
            "ACT_CODIGO": "",
            "ASESOR_WEB": 0,
            "COD_CIIU": "",
            "TIPTRIB_CCN": 0,
            "TIPTRIB_NOMBRE": "",
            "SECO_CODIGO": "",
            "SECO_NOMBRE": "",
            "BAN_CODIGO": "",
            "BAN_NOMBRE": "",
            "TER_TPOCNTA": "1",
            "TER_CUENTA": "",
            "TER_TIPONATU": "N",
            "JORLAB_CODIGO": "",
            "JORLAB_NOMBRE": "",
            "OCU_CODIGO": "",
            "OCU_NOMBRE": "",
            "TPS_CCN": "1",
            "TPS_DESCRIPCION": "",
            "OBSERVACIONES": "",
            "TER_TOTING": 0,
            "TER_TOTEGRE": 0,
            "TER_TOTACTIV": 0,
            "TER_TOTPASIV": 0,
            "TER_TOTPATRIM": 0,
            "TER_TOTCOST": 0,
            "CLI_MUJCABFAM": "",
            "AFECTA_VIVIENDA": "",
            "TER_ENVIOCORRESP": "",
            "COENT_CCN": 0,
            "COENT_NOMBRE": "",
            "CLI_SALARIO": 0,
            "CLI_OTRSEGRE": 0,
            "CTR_CODIGO": "",
            "CTR_NOMBRE": "",
            "CLI_ULFCHLIQU": null,
            "PRO_CODTER": 0,
            "PRO_NOMBRE": "",
            "PRO_NOMBRE_1": "",
            "PRO_CODIGO": 7,
            "TER_OTRING": 0,
            "TER_CONOTRING": "",
            "TER_RELPERPEP": "",
            "TIPVIV_CCN": 0,
            "CAR_NOMBRE": "",
            "NOM_CONY1": "",
            "NOM_CONY2": "",
            "APE_CONY1": "",
            "APE_CONY2": "",
            "DEP_CODDILI": "",
            "CIU_CODDILI": "",
            "TER_FCHDILI": null,
            "EMP_CODIGO": "",
            "EMP_NOMBRE": "",
            "TER_EMPLAB": "",
            "TER_RELLAB": "",
            "CIU_EMPLAB": "",
            "CIU_NOMBRE_1": "",
            "TER_DIREMPLAB": "",
            "TER_TELEMPLAB": "",
            "TER_CELEMPLAB": "",
            "TER_SUELDO": 0,
            "TER_FCHINEMPLAB": null,
            "TER_FCHTERCONT": null,
            "TER_TIPCONTRA": "",
            "CTR_NOMBRE_1": "",
            "NRO_TARVISIO": "",
            "NRO_TARVISIO1": "",
            "PAI_PK": 0,
            "PAI_NAC": 170,
            "PAIS_CODIGO": "",
            "TER_AUTTRATDATOS": "",
            "TER_CCNASESOR": 0,
            "CIIU_CODIGO_2": "",
            "CIIU_NOMBRE": "",
            "SECO_CODIGO_2": "",
            "SECO_NOMBRE_1": "",
            "TER_TOTEXCED": 0,
            "TER_DIGITOVERIF": "",
            "VALORCOND": 0,
            "TEL_CONY": "",
            "EMAIL_CONY": "",
            "NIVEL_EST_CONY": "",
            "ACTIVIDAD_CONY": "",
            "TER_SINANLUCR": "",
            "TER_SIGLA": "",
            "TIENSAL_CCODIGO": 0,
            "TER_SOCIO_S_N": "S",
            "TER_TELEFONOLAB": "",
            "CIU_CODIGOLAB": "",
            "TER_DIRECCIONLAB": "",
            "COD_CIIU_1": "",
            "CIIU_NOMBRE_1": "",
            "TER_TOTCOSTOP": 0,
            "ESTRATO_CONY": "",
            "TER_RAZONSOCIAL": "",
            "CLI_FCHINGRESOCIA": null,
            "TER_NOMCOMPLETO": ""
        }
    ]'
]
}

预期: 有效的JSON。我不知道是否还有更多步骤,我试图在Django上使用该代码。它只显示一个完整的字符串。

2 个答案:

答案 0 :(得分:3)

如果服务器确实发送了.json()响应,请确保json将正确执行此工作。但是里面的Results似乎也确实是json

如果您不是开发服务器端的人,那么您可以做的是:

import requests
import json

response = requests.get('http://codehere.com', auth=('user', 'pass'), content_type='application/json')
if response.status_code == 200:
    resp = response.json()
    if 'result' in resp:
        result = json.loads(resp['result'])
    else:
        print('Something is wrong with result')
        print(resp)
else:
    print(response.status_code)
    print(response.content)

因此,现在您可以根据需要处理result

答案 1 :(得分:0)

两个打印输出均显示相同值的稍有不同。 该值是具有单个键'result'的字典,其值 似乎是一个字符串,包含一个包含单个字典的Python列表的表示形式。外观可能具有欺骗性。

这似乎表明服务器以我们称为次优方式的方式运行。如果您对此有任何控制,那将是解决此情况的合乎逻辑的地方。

从本质上讲,内容被多轮字符串严重破坏了 和/或JSON编码,但我不确定是否可以轻松修复此客户端。