python 属性名应该用双引号括起来:第1行第2列(字符1),即使所有属性都用双引号括起来,也会出现此错误

gwo2fgha  于 2024-01-05  发布在  Python
关注(0)|答案(2)|浏览(149)

当我尝试点击下面的JSON文件进行发布请求时,我得到了JSON解码错误。请帮助
API代码:

  1. import requests
  2. from utilities.configurations import *
  3. from utilities.resources import *
  4. #from PayLoads.plans.payload1A1C import *
  5. from PayLoads.plans.testpayload import *
  6. config = configparser.ConfigParser()
  7. config.read('utilities/properties.ini')
  8. url = getCOnfigurations()['API']['URL']+APIResources.endpoints_plans
  9. response = requests.post(url, json= plan1A1C(),)

个字符
对于上面的代码,我得到了以下错误:“json.decoder.JSONDecodeError:Expecting property name encountered in double quotes:line 1 column 2(char 1)"。我还检查了json中的属性值仅为inb双引号

nue99wik

nue99wik1#

尝试下面的方法(注意body str被定义为字符串)

  1. import json
  2. def foo():
  3. body_str = '''{
  4. "payload": {
  5. "data": {
  6. "organization_code": "TEST",
  7. "quote_id": 71728,
  8. "quote_token": "odQ53i3SxCANUR68Lry3",
  9. "session_token": "odQ53i3SxCANUR68Lry3",
  10. "partner_code": "TEST",
  11. "category_code": "HEALTH_RETAIL",
  12. "insured": [
  13. {
  14. "person_id": 45820,
  15. "name": null,
  16. "occupation": null,
  17. "annual_income": null,
  18. "phone_number": "8789538796",
  19. "email": null,
  20. "age": 40,
  21. "dob": "07-09-1981",
  22. "gender": null,
  23. "nominee_name": " ",
  24. "nominee_dob": null,
  25. "nominee_age": null,
  26. "nominee_gender": null,
  27. "appointee_name": " ",
  28. "appointee_dob": null,
  29. "relationship_with_appointee": null,
  30. "appointee_gender": null,
  31. "relationship_with_nominee": null,
  32. "relation_with_primary_member": "self",
  33. "height": null,
  34. "weight": null,
  35. "salutation": null,
  36. "marital_status": null
  37. },
  38. {
  39. "person_id": 45821,
  40. "name": null,
  41. "occupation": null,
  42. "annual_income": null,
  43. "phone_number": "8789538796",
  44. "email": null,
  45. "age": 10,
  46. "dob": "07-09-2011",
  47. "gender": null,
  48. "nominee_name": " ",
  49. "nominee_dob": null,
  50. "nominee_age": null,
  51. "nominee_gender": null,
  52. "appointee_name": " ",
  53. "appointee_dob": null,
  54. "relationship_with_appointee": null,
  55. "appointee_gender": null,
  56. "relationship_with_nominee": null,
  57. "relation_with_primary_member": "kid-1",
  58. "height": null,
  59. "weight": null,
  60. "salutation": null,
  61. "marital_status": null
  62. }
  63. ],
  64. "primary_member_age": 40,
  65. "sum_insured": 100000,
  66. "pin_code": "751008",
  67. "payment_frequency": "SINGLE",
  68. "payment_option": null,
  69. "limited_pay": null,
  70. "policy_paying_term": null,
  71. "tobacco_habit": null,
  72. "risk_profile": null,
  73. "maturity_benefit_type": null,
  74. "investment_goal": null,
  75. "investment_amount": null,
  76. "cover_list": null,
  77. "insurance_cover_list": null,
  78. "posp": {
  79. "name": "abcd",
  80. "user_id": "9991112223",
  81. "pan": null,
  82. "aadhar": null,
  83. "level": {
  84. "id": 41,
  85. "partner_id": 52,
  86. "role": "ANAND_RATHI_staff",
  87. "code": "ANAND_RATHI_POSP",
  88. "superior_role_id": null,
  89. "path": "//41",
  90. "created_at": "2021-06-11T17:48:01.000+05:30",
  91. "updated_at": "2021-06-12T16:42:40.000+05:30"
  92. },
  93. "staf_type": "POSP"
  94. },
  95. "product_code": "TEST",
  96. "insurance_code": "TEST",
  97. "tenure": 1
  98. },
  99. "config": {
  100. "coi_url": "https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PolicySchedule",
  101. "password": "in5uR@nceAnan0",
  102. "plan_url": "https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PremiumComputation",
  103. "username": "ptrn_anandinsurance",
  104. "token_url": "https://developer.TEST.com/endpoint/token",
  105. "grant_type": "password",
  106. "policy_url": "https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PolicyGeneration",
  107. "proposal_url": "https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/ProposalSave",
  108. "PaymentOption": "A",
  109. "header_password": "TEST",
  110. "header_username": "TEST",
  111. "IntermediaryCode": "2002954741760001",
  112. "UniqueTransactionID": "8765456711111111112232"
  113. }
  114. },
  115. "insurance_company_code": "TEST",
  116. "ic": "TEST",
  117. "insurance_code": "TEST",
  118. "product_code": "TEST",
  119. "category_code": "TEST",
  120. "callbackUrl": "https://api.iifl.TEST.com/api_gateway_callbacks/plans"
  121. }'''
  122. s1 = json.loads(body_str)
  123. return s1
  124. print(foo())

字符串
输出

  1. {'payload': {'data': {'organization_code': 'TEST', 'quote_id': 71728, 'quote_token': 'odQ53i3SxCANUR68Lry3', 'session_token': 'odQ53i3SxCANUR68Lry3', 'partner_code': 'TEST', 'category_code': 'HEALTH_RETAIL', 'insured': [{'person_id': 45820, 'name': None, 'occupation': None, 'annual_income': None, 'phone_number': '8789538796', 'email': None, 'age': 40, 'dob': '07-09-1981', 'gender': None, 'nominee_name': ' ', 'nominee_dob': None, 'nominee_age': None, 'nominee_gender': None, 'appointee_name': ' ', 'appointee_dob': None, 'relationship_with_appointee': None, 'appointee_gender': None, 'relationship_with_nominee': None, 'relation_with_primary_member': 'self', 'height': None, 'weight': None, 'salutation': None, 'marital_status': None}, {'person_id': 45821, 'name': None, 'occupation': None, 'annual_income': None, 'phone_number': '8789538796', 'email': None, 'age': 10, 'dob': '07-09-2011', 'gender': None, 'nominee_name': ' ', 'nominee_dob': None, 'nominee_age': None, 'nominee_gender': None, 'appointee_name': ' ', 'appointee_dob': None, 'relationship_with_appointee': None, 'appointee_gender': None, 'relationship_with_nominee': None, 'relation_with_primary_member': 'kid-1', 'height': None, 'weight': None, 'salutation': None, 'marital_status': None}], 'primary_member_age': 40, 'sum_insured': 100000, 'pin_code': '751008', 'payment_frequency': 'SINGLE', 'payment_option': None, 'limited_pay': None, 'policy_paying_term': None, 'tobacco_habit': None, 'risk_profile': None, 'maturity_benefit_type': None, 'investment_goal': None, 'investment_amount': None, 'cover_list': None, 'insurance_cover_list': None, 'posp': {'name': 'abcd', 'user_id': '9991112223', 'pan': None, 'aadhar': None, 'level': {'id': 41, 'partner_id': 52, 'role': 'ANAND_RATHI_staff', 'code': 'ANAND_RATHI_POSP', 'superior_role_id': None, 'path': '//41', 'created_at': '2021-06-11T17:48:01.000+05:30', 'updated_at': '2021-06-12T16:42:40.000+05:30'}, 'staf_type': 'POSP'}, 'product_code': 'TEST', 'insurance_code': 'TEST', 'tenure': 1}, 'config': {'coi_url': 'https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PolicySchedule', 'password': 'in5uR@nceAnan0', 'plan_url': 'https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PremiumComputation', 'username': 'ptrn_anandinsurance', 'token_url': 'https://developer.TEST.com/endpoint/token', 'grant_type': 'password', 'policy_url': 'https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/PolicyGeneration', 'proposal_url': 'https://developer.TEST.com/endpoint/Health-flexiretail/v1.0.0/ProposalSave', 'PaymentOption': 'A', 'header_password': 'TEST', 'header_username': 'TEST', 'IntermediaryCode': '2002954741760001', 'UniqueTransactionID': '8765456711111111112232'}}, 'insurance_company_code': 'TEST', 'ic': 'TEST', 'insurance_code': 'TEST', 'product_code': 'TEST', 'category_code': 'TEST', 'callbackUrl': 'https://api.iifl.TEST.com/api_gateway_callbacks/plans'}

展开查看全部
h9vpoimq

h9vpoimq2#

这里的问题似乎是Python将字典转换为字符串的方式。Python默认使用单引号将字典中的字符串转换为str()时的字符串。

  1. import json
  2. data = {"foo":"bar"}
  3. str(data)
  4. #"{'foo': 'bar'}"
  5. json.loads(str(data))
  6. # raises json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

字符串
这对于json.loads()来说是有问题的,因为单引号是不可接受的JSON format。也可以参考这个SO问题:Python/Json:Expecting property name enclosed in double quotes
相反,你应该使用json.dumps(),它将Python对象序列化为JSON格式的字符串:

  1. import json
  2. data = {"foo":"bar"}
  3. json.dumps(data)
  4. #'{"foo": "bar"}'
  5. json.loads(json.dumps(data))
  6. #{'foo': 'bar'}

展开查看全部

相关问题