簡體   English   中英

如何使用Javascript將JSON字符串轉換為具有其他結構的新JSON對象

[英]How to Convert JSON String into new JSON Object with other structure using Javascript

javascript無法讀取json字符串,幫助我將其轉換為javascript JSON對象

{
  text: {
    name: "ROOT"
  },
  HTMLclass: "blue",
  image: "images/no_member.png",
  children: [
    {
      connectors: {
        style: {
          stroke: "#000000"
        }
      },
      text: {
        name: "SAYED"
      },
      HTMLclass: "blue",
      image: "images/no_member.png",

    },
    {
      connectors: {
        style: {
          stroke: "#000000"
        }
      },
      text: {
        name: "DIDAR"
      },
      HTMLclass: "blue",
      image: "images/no_member.png",

    }
  ]
}

JSON.parse()可能是答案,您應該將鍵包含在“”中。
但是JSON(JavaScript對象表示法)的意思是“像Javascript對象一樣的表示法”。 所以本來想。 像下面這樣;

    var obj = {
        text: {
            name: "ROOT"
        },
        HTMLclass: "blue",
        image: "images/no_member.png",
        children: [
            {
                connectors: {
                    style: {
                        stroke: "#000000"
                    }
                },
                text: {
                    name: "SAYED"
                },
                HTMLclass: "blue",
                image: "images/no_member.png",

            },
            {
                connectors: {
                    style: {
                        stroke: "#000000"
                    }
                },
                text: {
                    name: "DIDAR"
                },
                HTMLclass: "blue",
                image: "images/no_member.png",

            }
        ]
    };
    console.log(obj);

謝謝。

您需要JSON.parse()。

有關更多詳細信息,請參見JSON

暫無
暫無

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

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