本文介绍了将列表转换为 json 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个巨大的文本文件.
I have a huge text file.
line 1
line 2
line 3
...
我已将其转换为列表数组:
I have converted it into an array of lists:
[['String 1'],['String 2'],['String 3'],['String 4'],['String 5'],
['String 6'],['String 7'],['String 8'],['String 9'],['String 9'],
['String 10'], ...]
我想将此列表转换为 JSON 对象,如下所示:
I want to convert this list to JSON objects, like this:
[{'title1': 'String 1', 'title2': 'String 2', ... , 'title7': 'String 7'},
{'title1': 'String 8', ..., 'title7': 'String 14'}, ...]
我不知道该怎么做.有什么帮助吗?
I am not sure how to do it. Any help.?
推荐答案
只需添加到alexce的响应中,您就可以轻松地将重组后的数据转换为JSON:
Just adding onto alexce's response, you can easily convert the restructured data into JSON:
import json
json.dumps(result)
顶部存在一些潜在的安全问题水平阵列.我不确定它们在现代浏览器中是否仍然有效,但您可能需要考虑将其包装在一个对象中.
There are some potential security concerns with top-level arrays. I'm not sure if they're still valid with modern browsers, but you may want to consider wrapping it in an object.
import json
json.dumps({'results': result})
这篇关于将列表转换为 json 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!