0

Json string can't read by javascript help me to convert it javascript Json object

{
  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",

    }
  ]
}
1
  • Are you asking how to type this in so that JavaScript will recognize it as a valid object as shown in @C.Koo's answer or are you asking how to programmatically convert a string you have received into a JavaScript object? Commented Jul 23, 2017 at 8:10

2 Answers 2

1

JSON.parse() may be answer, and you should envelop keys in "".
But JSON(JavaScript Object Notation) means "Notation like Javascript Object". So think originally. Like following;

    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);

Thank you.

Sign up to request clarification or add additional context in comments.

2 Comments

please see my previous post stackoverflow.com/questions/45205383/… when i am generation json string in variable when i am converting json object i got below error Uncaught SyntaxError: Unexpected token e in JSON at position 1 at JSON.parse (<anonymous>)
@Md. Didarul Islam, I'm sure that the first key(I mean {key:value}) is begun with e. When we use JSON.parse(), all keys must be enveloped in "". Thank you for your comment.
-1

You want JSON.parse().

See JSON for more details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.