使用json.loads解析含HTML的JSON字符串遇JSONDecodeError求助
JSONDecodeError 解析含HTML双引号的JSON字符串解决方案
问题场景
解析JSON字符串时触发以下错误:
JSONDecodeError at /octopus/entries/53/test_sample_job/
Expecting ',' delimiter: line 3 column 27 (char 48)
问题出在待解析的JSON字符串变量data中,content字段包含未转义的HTML双引号,导致JSON格式无效。待解析的data内容如下:
data = { "name": "Shea", "content": "<p style="text-align:left">Job Description</p><p style="text-align:inherit"></p>Have you heard about phenom? phenom is an innovative, global healthcare leader committed to improving health and well-being in 140 countries around the world. We continue to focus our research on conditions that affect millions of people around the world - diseases like Alzheimer's, Diabetes and Cancer - while further expanding our strengths in areas such as vaccines and biologics. We aspire to be the best healthcare company in the world and are dedicated to providing leading innovations and solutions for tomorrow. phenom’s Global Human Health (GHH) Division abides by a “patient first, profits later” ideology. Results-driven and ambitious, this team of individuals represents a functional balance between meeting company objectives and the needs of people around the world. The division is comprised of sales and marketing professionals who are passionate about their role in bringing phenom's prescription medicines, vaccines, and other medical products to our customers worldwide. Who are we looking for? A strong Professional for the position of the Hospital Specialist in Oncology who is responsible for promoting oncology brands within given accounts. On this position you would need to understand customers’ needs and have strong business acumen. It is important YOU are being equipped with excellent medical knowledge where you can transfer key medical data into customer / patient benefits. Expected Qualification of YOURS: - University degree preferably life science - 3+ years of experience working in a customer-facing role - Strong knowledge of customer/business strategy - Understanding of local healthcare and reimbursement systems - English language preferred Key competencies: - Customer & Market Insights: Ability to develop a deep understanding of customer needs, behaviours and goals, as well as market dynamics, competitor analysis and trends to improve overall business outcomes. - Customer Engagement: Ability to identify and appropriately build and maintain long-term, sustainable relationships with customers, external stakeholders and key influencers through a variety of relationship-building approaches. - Strategic Business Management: Ability to set strategic plans, consider execution trade-offs and continuously adjust approaches to maximize business performance and increase sales. - Excellent medical Product knowledge – excellent evidence-basedmedicine data knowledge. Ability to transfer medical data into customer / patients benefits Skills Required: - Driving license B - Advance Medical knowledge of oncology therapy area preferably or demonstrate high learning agility and interest in evidence-based medicine data and ability to transfer them into customer / patients benefits Leadership behaviours: - Drive result - Focus on customer and patient - Demonstrate ethics and integrity - High learning agility YOUR primary activities include but are not limited to: Account Understanding and Analysis - Understanding decision-making processes within the account, patient flow - Identifying Account Stakeholders and understanding their perspectives on phenom, our competitors - Completing a competitor analysis for the account - Obtaining an in-depth understanding of the account’s unmet and evolving needs Account Plan Development - Identifying short and long-term business opportunities. - Defining objectives for the account - Developing a plan for the Account that contains the account needs and perspectives as well as considers competitive and business challenges - Determining how to appropriately leverage cross-functional internal resources to maximize potential - Defining account metrics and a tracking plan Account Plan Implementation and Tracking - Developing and maintaining long-term engagements with customers/stakeholders within the Accounts that are responsible for treatment of the respective patients (all relevant HCPs) as well as product purchasing (hospital management, pharmacists) - Conducting product and value-based negotiation <p style="text-align:inherit"></p><p style="text-align:left"><b>English Job Description:</b></p><p style="text-align:inherit"></p><p style="text-align:inherit"></p><p></p><p><b>Search Firm Representatives Please Read Carefully </b><br>phenom & Co., Inc., Kenilworth, NJ, USA, also known as phenom phenom & phenom Corp., Kenilworth, NJ, USA, does not accept unsolicited assistance from search firms for employment opportunities. All CVs / resumes submitted by search firms to any employee at our company without a valid written search agreement in place for this position will be deemed the sole property of our company. No fee will be paid in the event a candidate is hired by our company as a result of an agency referral where no pre-existing agreement is in place. Where agency agreements are in place, introductions are position specific. Please, no phone calls or emails. </p><p style="text-align:inherit"></p><p style="text-align:left"><b>Employee Status: </b></p>Regular<p style="text-align:inherit"></p><p style="text-align:left"><b>Relocation:</b></p><p style="text-align:inherit"></p><p style="text-align:left"><b>VISA Sponsorship:</b></p><p style="text-align:inherit"></p><p style="text-align:left"><b><span>Travel Requirements:</span></b></p><p style="text-align:inherit"></p><p style="text-align:left"><b>Flexible Work Arrangements:</b></p><p style="text-align:inherit"></p><p style="text-align:left"><b>Shift:</b></p><p style="text-align:inherit"></p><p style="text-align:left"><b>Valid Driving License:</b></p><p style="text-align:inherit"></p><p style="text-align:left"><b>Hazardous Material(s):</b></p><p style="text-align:inherit"></p><p style="text-align:left"><b>Number of Openings: </b></p>1", "street_name": "Bartol", "city": "Brentwood", "country": "Slovenia", "continent": "Europe" }
已尝试的无效方案:
- 使用
json.loads(repr(data))、html.escape(json.loads(data)),报错未解决 - 替换所有双引号为空字符串,触发
too many values to unpack错误 - 尝试对变量
data使用r前缀,但不知如何操作
可行解析方案
方案1:修复JSON字符串的转义问题
JSON规范要求字符串内部的双引号必须用反斜杠\转义。将content字段中HTML属性的双引号全部转义,比如<p style="text-align:left">改为<p style=\"text-align:left\">。
修正后的data示例:
data = '''{ "name": "Shea", "content": "<p style=\"text-align:left\">Job Description</p><p style=\"text-align:inherit\"></p>...", "street_name": "Bartol", "city": "Brentwood", "country": "Slovenia", "continent": "Europe" }''' result = json.loads(data)
方案2:用原始字符串(r前缀)配合引号嵌套
在Python中定义JSON字符串时,使用三引号+原始字符串组合,减少手动转义的工作量:
# 用单引号包裹整个JSON,内部HTML双引号需转义 data = r'''{ "name": "Shea", "content": "<p style=\"text-align:left\">Job Description</p>...", "street_name": "Bartol", "city": "Brentwood", "country": "Slovenia", "continent": "Europe" }''' # 或者修改HTML属性为单引号(HTML兼容),用双引号包裹整个JSON data = r"""{ "name": "Shea", "content": '<p style="text-align:left">Job Description</p>...', "street_name": "Bartol", "city": "Brentwood", "country": "Slovenia", "continent": "Europe" }""" result = json.loads(data)
方案3:预处理字符串修复未转义双引号
如果无法修改原始data定义,用正则表达式定位content字段内的未转义双引号并转义:
import json import re # 正则匹配content字段内的未转义双引号并替换为\" fixed_data = re.sub(r'(?<="content": ".*?)\"(?=.*?")', r'\\"', data, flags=re.DOTALL) result = json.loads(fixed_data)
方案4:直接使用Python字典
如果数据由你生成,无需使用JSON字符串,直接定义Python字典,避免转义问题:
data_dict = { "name": "Shea", "content": "<p style='text-align:left'>Job Description</p>...", # 用单引号替代双引号 "street_name": "Bartol", "city": "Brentwood", "country": "Slovenia", "continent": "Europe" }
内容的提问来源于stack exchange,提问作者Mahesh
相关产品推荐
相关产品推荐

