【发布时间】:2018-10-06 14:48:08
【问题描述】:
在 python 中,我无法弄清楚如何将 JSON 对象(如下所示)输出为字符串,其中 Baseball 的内容基于“key1”(降序)排序。当我收到 JSON(来自数据源)时,播放器出现故障。最终,我的代码需要对播放器进行排序,然后将其传递给下一个排序的函数。请假设我无法将 JSON 的格式修改为/拥有数组,因为消费函数无法处理(因为它目前是编写的)。
示例 JSON:
{
"DataSource1":{
"Baseball":{
"Sean":{
"key1":"10",
},
"Gene":{
"key1":"100",
},
"Alan":{
"key1":"1",
}
}
},
"DataSource2":{
"Baseball":{
"Bob_Smith":{
"key1":"1"
},
"Adam_Filmore":{
"key1":"100"
},
"Joe_Allen":{
"key1":"10"
}
}
}
"DataSource3":{
"Baseball":{
"Jake":{
"key1":"10"
},
"Huck":{
"key1":"1"
},
"Eric":{
"key1":"100"
}
}
}
}
我希望 JSON 如何输出的示例:
{
"DataSource1":{
"Baseball":{
"Alan":{
"key1":"1",
},
"Sean":{
"key1":"10",
},
"Gene":{
"key1":"100",
}
}
},
"DataSource2":{
"Baseball":{
"Bob_Smith":{
"key1":"1"
},
"Joe_Allen":{
"key1":"10"
},
"Adam_Filmore":{
"key1":"100"
}
}
}
"DataSource3":{
"Baseball":{
"Huck":{
"key1":"1"
},
"Jake":{
"key1":"10"
},
"Eric":{
"key1":"100"
}
}
}
}
【问题讨论】:
-
对象属性在 Javascript 中没有顺序。您应该将属性复制到数组中,然后对其进行排序。
标签: python json string sorting