python 解析json字符

发布于 2022-09-12 23:25:15 字数 5783 浏览 11 评论 0

字符如下
{"data":{"question":{"questionId":"1","content":"<p>Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.</p>\n\n<p>You may assume that each input would have exactly one solution, and you may not use the same element twice.</p>\n\n<p>You can return the answer in any order.</p>\n\n<p> </p>\n<p>Example 1:</p>\n\n<pre>\nInput: nums = [2,7,11,15], target = 9\nOutput: [0,1]\nOutput: Because nums[0] + nums[1] == 9, we return [0, 1].\n</pre>\n\n<p>Example 2:</p>\n\n<pre>\nInput: nums = [3,2,4], target = 6\nOutput: [1,2]\n</pre>\n\n<p>Example 3:</p>\n\n<pre>\nInput: nums = [3,3], target = 6\nOutput: [0,1]\n</pre>\n\n<p> </p>\n<p>Constraints:</p>\n\n<ul>\n\t<li>2 <= nums.length <= 103</li>\n\t<li>-109 <= nums[i] <= 109</li>\n\t<li>-109 <= target <= 109</li>\n\t<li>Only one valid answer exists.</li>\n</ul>\n","translatedTitle":"\u4e24\u6570\u4e4b\u548c","translatedContent":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums\u00a0\u548c\u4e00\u4e2a\u6574\u6570\u76ee\u6807\u503c target\uff0c\u8bf7\u4f60\u5728\u8be5\u6570\u7ec4\u4e2d\u627e\u51fa \u548c\u4e3a\u76ee\u6807\u503c \u7684\u90a3\u00a0\u4e24\u4e2a\u00a0\u6574\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u6570\u7ec4\u4e0b\u6807\u3002</p>\n\n<p>\u4f60\u53ef\u4ee5\u5047\u8bbe\u6bcf\u79cd\u8f93\u5165\u53ea\u4f1a\u5bf9\u5e94\u4e00\u4e2a\u7b54\u6848\u3002\u4f46\u662f\uff0c\u6570\u7ec4\u4e2d\u540c\u4e00\u4e2a\u5143\u7d20\u5728\u7b54\u6848\u91cc\u4e0d\u80fd\u91cd\u590d\u51fa\u73b0\u3002</p>\n\n<p>\u4f60\u53ef\u4ee5\u6309\u4efb\u610f\u987a\u5e8f\u8fd4\u56de\u7b54\u6848\u3002</p>\n\n<p>\u00a0</p>\n\n<p>\u793a\u4f8b 1\uff1a</p>\n\n<pre>\n\u8f93\u5165\uff1anums = [2,7,11,15], target = 9\n\u8f93\u51fa\uff1a[0,1]\n\u89e3\u91ca\uff1a\u56e0\u4e3a nums[0] + nums[1] == 9 \uff0c\u8fd4\u56de [0, 1] \u3002\n</pre>\n\n<p>\u793a\u4f8b 2\uff1a</p>\n\n<pre>\n\u8f93\u5165\uff1anums = [3,2,4], target = 6\n\u8f93\u51fa\uff1a[1,2]\n</pre>\n\n<p>\u793a\u4f8b 3\uff1a</p>\n\n<pre>\n\u8f93\u5165\uff1anums = [3,3], target = 6\n\u8f93\u51fa\uff1a[0,1]\n</pre>\n\n<p>\u00a0</p>\n\n<p>\u63d0\u793a\uff1a</p>\n\n<ul>\n\t<li>2 <= nums.length <= 103</li>\n\t<li>-109 <= nums[i] <= 109</li>\n\t<li>-109 <= target <= 109</li>\n\t<li>\u53ea\u4f1a\u5b58\u5728\u4e00\u4e2a\u6709\u6548\u7b54\u6848</li>\n</ul>\n","similarQuestions":"[{\"title\": \"3Sum\", \"titleSlug\": \"3sum\", \"difficulty\": \"Medium\", \"translatedTitle\": \"\u4e09\u6570\u4e4b\u548c\"}, {\"title\": \"4Sum\", \"titleSlug\": \"4sum\", \"difficulty\": \"Medium\", \"translatedTitle\": \"\u56db\u6570\u4e4b\u548c\"}, {\"title\": \"Two Sum II - Input array is sorted\", \"titleSlug\": \"two-sum-ii-input-array-is-sorted\", \"difficulty\": \"Easy\", \"translatedTitle\": \"\u4e24\u6570\u4e4b\u548c II - \u8f93\u5165\u6709\u5e8f\u6570\u7ec4\"}, {\"title\": \"Two Sum III - Data structure design\", \"titleSlug\": \"two-sum-iii-data-structure-design\", \"difficulty\": \"Easy\", \"translatedTitle\": \"\u4e24\u6570\u4e4b\u548c III - \u6570\u636e\u7ed3\u6784\u8bbe\u8ba1\"}, {\"title\": \"Subarray Sum Equals K\", \"titleSlug\": \"subarray-sum-equals-k\", \"difficulty\": \"Medium\", \"translatedTitle\": \"\u548c\u4e3aK\u7684\u5b50\u6570\u7ec4\"}, {\"title\": \"Two Sum IV - Input is a BST\", \"titleSlug\": \"two-sum-iv-input-is-a-bst\", \"difficulty\": \"Easy\", \"translatedTitle\": \"\u4e24\u6570\u4e4b\u548c IV - \u8f93\u5165 BST\"}, {\"title\": \"Two Sum Less Than K\", \"titleSlug\": \"two-sum-less-than-k\", \"difficulty\": \"Easy\", \"translatedTitle\": \"\u5c0f\u4e8e K \u7684\u4e24\u6570\u4e4b\u548c\"}]","topicTags":[{"name":"Array","slug":"array","translatedName":"\u6570\u7ec4"},{"name":"Hash Table","slug":"hash-table","translatedName":"\u54c8\u5e0c\u8868"}],"hints":["A really brute force way would be to search for all possible pairs of numbers but that would be too slow. Again, it's best to try out brute force solutions for just for completeness. It is from these brute force solutions that you can come up with optimizations.","So, if we fix one of the numbers, say <pre>x</pre>, we have to scan the entire array to find the next number <pre>y</pre> which is <pre>value - x</pre> where value is the input parameter. Can we change our array somehow so that this search becomes faster?","The second train of thought is, without changing the array, can we use additional space somehow? Like maybe a hash map to speed up the search?"]}}}

用php的json_decode 就没问题
但是用py的
image.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏拉图鍀咏恒 2022-09-19 23:25:15

有一个包直接叫做json,我是用它的,你可以试一下。

撧情箌佬 2022-09-19 23:25:15

decode的对象是字符串,转成python对象
要想把字典转字符串,你得用encode

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文