首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将字符串转换为json

将字符串转换为json
EN

Stack Overflow用户
提问于 2022-03-03 00:55:38
回答 1查看 77关注 0票数 1

我相信下面的字符串是json格式的,但是当我尝试将字符串加载到json变量中时,我可以轻松地查询和提取数据点,这样就会出错。

代码语言:javascript
复制
jdata = '<pre>{\n    "askId": "AAABB110-000011",\n    "dateCreated": "2009-09-01T00:00:00.000Z",\n    "dateUpdated": "2021-06-24T00:00:00.000Z",\n    "owners": [\n        {\n            "ownerType": "Service-Level Owner",\n            "VendorId": "000111222"\n        },\n        {\n            "ownerType": "Technical Owner",\n            "CustomerId": "000333444"\n        },\n        {\n            "ownerType": "Business Owner",\n            "ServiceId": "000005556"\n        }\n    ],\n    "createdBy": "SYSTEM",\n    "lastUpdatedBy": "000667778",\n    "applicationName": "Treasury Bank Data",\n    "description": "Process Data",\n    "aliases": [\n        "Treasury Bank Data",\n        "Bank Data",\n        "Bank Reconciliation",\n        "Bank Rec",\n        "SERV-X"\n    ],\n    "billingBusinessSegmentId": 6,\n    "category": {\n        "categoryId": 1,\n        "categoryName": "Application"\n    },\n    "lifecycleStage": {\n        "lifecycleStageId": 3,\n        "lifecycleStageName": "Production"\n    },\n    "acquiredEntity": {\n        "acquiredEntityId": 0,\n        "acquiredEntityName": "Not Applicable"\n    },\n    "enclaveEnvironment": {\n        "enclaveEnvironmentId": 0,\n        "enclaveEnvironmentName": "General Hosting (Internal, Cloud, or Vendor hosted)"\n    },\n    "softwareType": {\n        "softwareTypeId": 7,\n        "softwareTypeName": "Vendor Product"\n    },\n    "infrastructureRequired": false,\n    "uhgHelpdeskRequired": false,\n    "references": [\n        {\n            "referenceType": "disaster-recovery",\n            "referenceValue": "APPX009919"\n        }\n    ]\n}\n</pre>'

jsonData = json.loads(jdata)

一旦我试图将数据加载到jsonData变量中,下面就会出现错误。

代码语言:javascript
复制
Expecting value: line 1 column 1 (char 0)

我的字符串不允许作为json变量加载有什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-03 01:12:17

如果去掉html标记,就会得到json:

代码语言:javascript
复制
import json

jdata = '<pre>{\n    "askId": "AAABB110-000011",\n    "dateCreated": "2009-09-01T00:00:00.000Z",\n    "dateUpdated": "2021-06-24T00:00:00.000Z",\n    "owners": [\n        {\n            "ownerType": "Service-Level Owner",\n            "VendorId": "000111222"\n        },\n        {\n            "ownerType": "Technical Owner",\n            "CustomerId": "000333444"\n        },\n        {\n            "ownerType": "Business Owner",\n            "ServiceId": "000005556"\n        }\n    ],\n    "createdBy": "SYSTEM",\n    "lastUpdatedBy": "000667778",\n    "applicationName": "Treasury Bank Data",\n    "description": "Process Data",\n    "aliases": [\n        "Treasury Bank Data",\n        "Bank Data",\n        "Bank Reconciliation",\n        "Bank Rec",\n        "SERV-X"\n    ],\n    "billingBusinessSegmentId": 6,\n    "category": {\n        "categoryId": 1,\n        "categoryName": "Application"\n    },\n    "lifecycleStage": {\n        "lifecycleStageId": 3,\n        "lifecycleStageName": "Production"\n    },\n    "acquiredEntity": {\n        "acquiredEntityId": 0,\n        "acquiredEntityName": "Not Applicable"\n    },\n    "enclaveEnvironment": {\n        "enclaveEnvironmentId": 0,\n        "enclaveEnvironmentName": "General Hosting (Internal, Cloud, or Vendor hosted)"\n    },\n    "softwareType": {\n        "softwareTypeId": 7,\n        "softwareTypeName": "Vendor Product"\n    },\n    "infrastructureRequired": false,\n    "uhgHelpdeskRequired": false,\n    "references": [\n        {\n            "referenceType": "disaster-recovery",\n            "referenceValue": "APPX009919"\n        }\n    ]\n}\n</pre>'

jsonData = json.loads(jdata.lstrip('<pre>').rstrip('</pre>'))
print(jsonData)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71330930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档