【发布时间】:2021-02-27 07:15:08
【问题描述】:
关于 Json 列表操作的快速问题。
我有以下格式的 json 数据
[
"fruits",
"apple",
"{\n \"color\": \"red\",\n \"harvest\": \"ready\"\n}\n",
"{\n \"color\": \"green\",\n \"harvest\": \"not ready\"\n}\n",
"veggies",
"spinach"
"{\n \"color\": \"green\",\n \"harvest\": \"not ready\"\n}\n",
"{\n \"color\": \"light green\",\n \"harvest\": \"not ready\"\n}\n"
]
我想将“{”之前的任何字符串(例如:apple and spinach)带入“Key-Value”,并在 json 中添加一个静态“keys”,如“name”和“features”,最后它会看起来像这样:
{
"fruits":{"name":"apple",
"features":[
{
"color":"red",
"harvest":"ready"
}",
{"color":"green"
"harvest":"not ready"
]},
"veggies":{"name":"spinach",
"features":[
{
"color":"green",
"harvest":"notready"
},
{
"color":"light green",
"harvest":"not ready"
]}
}
【问题讨论】:
-
你如何得到这个 JSON?这是在一个json文件中吗?还是通过字符串存储在变量中?我问是因为你有“\n”回车。
-
我通过字符串得到
-
每个元素都在自己的行上吗?
-
是的,每个元素单独一行。感谢您的检查
标签: python arrays json sorting