skip to Main Content

I want to change string to variable, please some one help me.
this is a string >> {"method":"setValue","params":{"cmd_Motor":true}}

I wan to change like this:

2

Answers


  1. Use JSON.parse()

    const input = '{"method":"setValue","params":{"cmd_Motor":true}}';
    const result = JSON.parse(input);
    
    Login or Signup to reply.
  2. I am not sure if I understood your question, but you can convert a string to a JSON object and you can also convert a JSON object to a string:

    converting from string to json

    const myString = '{"method":"setValue","params":{"cmd_Motor":true}}';
    const myJson = JSON.parse(myString);
    
    console.log(myJson.method) // setValue
    console.log(myJson.params.cmd_Motor) // true

    converting from json to string

    const myJson = { method: 'setValue', params: { cmd_Motor: true } };
    const myString = JSON.stringify(myJson);
    
    console.log(myString);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search