温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - I'm trying to parse a JSON correctly with Python/Django

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

发布于 2020-03-27 11:35:35

我正在尝试使用JSON带有请求的数据,但在尝试验证时始终收到解析错误。

我尝试了这段代码,从转换dictstring,然后转换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上使用该代码。它只显示一个完整的字符串。

查看更多

查看更多

提问者
Cesar Cotiz
被浏览
58
edilio 2019-07-04 02:38

.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

发布
问题

分享
好友

手机
浏览

扫码手机浏览