2

i want to pass variable values into json below is what i am trying.

In javascript i am getting values like this:

var f_name= document.getElementById("firstname").value;
var phone_number= document.getElementById("number").value;

And then in JSON i want to pass these values:

"Site": {
   "Name": "+f_name+", 
   "Phone": "+phone_number+"
 }

its not working. What is wrong with this?

1 Answer 1

3

Create an object, then stringify it as JSON

var f_name       = document.getElementById("firstname").value;
var phone_number = document.getElementById("number").value;

var obj = {
    Name  : f_name,
    Phone : phone_number
}

var json = JSON.stringify(obj);

Chances are however that you haven't understood the difference between an object and a JSON string, and that you don't really want JSON at all.

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

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.