回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我一直在寻找答案,但是没有一个答案特别有用。我有一个必须转换成JSON字符串的值字典。数据如下:</p>
<pre><code>In [127]: ddd
Out[127]:
{'ID': {'condition': '',
'data': {1: '2',
2: '3',
3: '4',
4: '5',
5: '6',
6: '7',
7: '8',
8: '9',
9: '10'},
'desc_long': 'Id szko\xc5\x82y',
'desc_short': 'Id szko\xc5\x82y',
'df_name': 'pierszytest',
}}
</code></pre>
<p>当我试图执行json.dumps文件(ddd)我得到:</p>
^{pr2}$
<p>我创建了一个测试变量“what”</p>
<pre><code>In [126]: what
Out[126]: {1: '2', 2: '3', 3: '4', 4: '5', 5: '6', 6: '7', 7: '8', 8: '9', 9: '10'}
</code></pre>
<p>在json.dumps文件(什么)回报:</p>
<pre><code>Out[129]: '{"1": "2", "2": "3", "3": "4", "4": "5", "5": "6", "6": "7", "7": "8", "8": "9", "9": "10"}'
</code></pre>
<p>再次,我试图只转换ddd['ID']['data']:</p>
<pre><code> In [131]: ddd['ID']['data']
Out[131]: {1: '2', 2: '3', 3: '4', 4: '5', 5: '6', 6: '7', 7: '8', 8: '9', 9: '10'}
In [130]: json.dumps(ddd['ID']['data'])
TypeError: keys must be a string
</code></pre>
<p>所以,这基本上是相同的变量json.dumps文件不能处理后者。这对我来说是个大惊喜。我做了一些研究,但对我的案子没有任何帮助。如果是复制品,请告诉我。在</p>
<p><strong>*编辑*</strong>
我在两个变量上附加了type()的结果:</p>
<pre><code>In [132]: type(ddd['ID']['data'])
Out[132]: dict
In [133]: type(what)
Out[133]: dict
</code></pre>