python 處理 {'message': 'JSON parse error - Expecting value: line 1 column 1 (char 0)', 'code': 'parse_error'}


python 處理 {'message': 'JSON parse error - Expecting value: line 1 column 1 (char 0)', 'code': 'parse_error'}

 

今天在運行自動化的過程中,遇到了 {'message': 'JSON parse error - Expecting value: line 1 column 1 (char 0)', 'code': 'parse_error'}這個的error,各種嘗試無果:

請求的數據為json格式:

data = {'code_type': 'send', 'category': 1, 'phone': '13123456784'}

發送請求:requests.post(url,data)

返回結果:{'message': 'JSON parse error - Expecting value: line 1 column 1 (char 0)', 'code': 'parse_error'}

 

之前遇到過,嘗試把json格式的數據轉換為string,就解決了,今天怎么處理都不行,沒法了,向萬能的度娘求助,終於解決:

  借助第三方包的幫助,這里使用了demjson的包來處理這個問題

     安裝: pip install demjson

     使用: demjson.encode(data)

 demjson有兩個主要的方法:

  •   encode  編碼,將對象轉換為json
  •   decode   解碼,將json轉化為對象

python單元測試

import json
import random
import uuid

import demjson
from django.test import TestCase
from rest_framework.test import APIClient
from back_server.models import CertificationSource


class LittleTestCase(TestCase):
    def setUp(self):
        self.client = APIClient()

        self.test_cs = CertificationSource.objects.create(**{"id":uuid.uuid1(node=random.randint(0,281474976710655))
                                                             ,"cs_name": "ni_ldap_test00",
                                                             "hosts": "LDAP://navinfo.ldap.com;LDAPS://navinfo.ldap.com",
                                                             "base_dn": "OU=ww,DC=aa,DC=ss",
                                                             "user_filter": "dsdsdfa",
                                                             "remark": "test",
                                                             "user_cn": "CN=users,OU=xx,DC=xx,DC=com",
                                                             "state": False})
        self.test_cs1 = CertificationSource.objects.create(**{"id":uuid.uuid1(node=random.randint(0,281474976710655)),
                                                              "cs_name": "ni_ldap_test08",
                                                             "hosts": "LDAP://navinfo.ldap.com;LDAPS://navinfo.ldap.com",
                                                             "base_dn": "OU=ww,DC=aa,DC=ss",
                                                             "user_filter": "dsdsdfa",
                                                             "remark": "test",
                                                             "user_cn": "CN=users,OU=xx,DC=xx,DC=com",
                                                             "state": False})
        self.test_cs2 = CertificationSource.objects.create(**{"id": uuid.uuid1(node=random.randint(0, 281474976710655)),
                                                              "cs_name": "ni_ldap_test09",
                                                              "hosts": "LDAP://navinfo.ldap.com;LDAPS://navinfo.ldap.com",
                                                              "base_dn": "OU=ww,DC=aa,DC=ss",
                                                              "user_filter": "dsdsdfa",
                                                              "remark": "test",
                                                              "user_cn": "CN=users,OU=xx,DC=xx,DC=com",
                                                              "state": False})


    def test_batch_delete_cs(self):
        cs_id_list= [str(self.test_cs2.id),str(self.test_cs1.id)]
        print(cs_id_list,'cs_id_list')
        import demjson
        last_cs_id_list=demjson.encode(cs_id_list)
        print(cs_id_list,'idlis_json----',type(cs_id_list),'type====')
        r = self.client.post(path="/iam/api/v1/batch/certification?option=" + "delete", data=last_cs_id_list,content_type='application/json')
        result = r.json()
        self.assertEqual(result['result']['data']['delete_success_list'], cs_id_list)

打印結果

['2963f0c6-5cf2-11ea-beb1-a93aec2284b4', '2963c8e6-5cf2-11ea-9a9c-863031c2bb23'] cs_id_list
['2963f0c6-5cf2-11ea-beb1-a93aec2284b4', '2963c8e6-5cf2-11ea-9a9c-863031c2bb23'] idlis_json---- <class 'list'> type====

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM