将列表转换为json对象 [英] Convert a list to json objects

查看:152
本文介绍了将列表转换为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对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆