簡體   English   中英

使用 Python 將 Json 轉換為 JS Object

[英]Convert Json to JS Object with Python

我有一些 excel 文件。 我想將它們轉換為 JS 對象。 我可以轉換為 Json 如下所示。

{ "profileMenu.info": "Profile information",
        "profileMenu.infos.generalInfo": "general information",
        "profileMenu.infos.otherInfos": "Other information" }

我需要像這樣轉換它們。

  profileMenu: {
    info: 'Profile information',
    infos: {
      generalInfo: 'general information',
      otherInfos: 'Other information',
    } 

我已經搜索了執行此操作的方法,但找不到任何方法。 您能幫我了解如何將此 Json 轉換為 JS 對象嗎?

如果您願意使用外部庫,

在 javascript 中,您可以使用lodash執行此操作:

 var myJSON = { "profileMenu.info": "Profile information", "profileMenu.infos.generalInfo": "general information", "profileMenu.infos.otherInfos": "Other information" } let myObject = {} for (key in myJSON) { _.set(myObject, key, myJSON[key]); } console.log(myObject)
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

在 Python 中,您可以使用pydash執行此操作:

import pydash

myJSON = {
  "profileMenu.info": "Profile information",
  "profileMenu.infos.generalInfo": "general information",
  "profileMenu.infos.otherInfos": "Other information"
}

myObject = {}

for key in myJSON:
  pydash.set_(myObject, key, myJSON[key])
  
print(myObject)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM