0

Hello I am trying to create a JSON object using some values I take from a form. For some reson the code isn't working. I triple checked all the names f the inputs so you can assume they are correct. I did isolate the problem to the line where I instantiate my JSON object:

    function createJSON()
    {
        if (document.forms["formIn"]["gender"][0].checked == true)
        {
            var g= "male";
        }
        else if (document.forms["formIn"]["gender"][1].checked == true)
        {
            var g= "male";
        }
        var jsonobj={"name":document.forms["formIn"]["name"].value, "surname":document.forms["formIn"]["surname"].value, "email":document.forms["formIn"]["email"], "dob":document.forms["formIn"]["dob"].value, "password":document.forms["formIn"]["password"].value, "cpassword":document.forms["formIn"]["confirm_password"].value, "gender":g}
        var jsonstr=JSON.stringify(jsonobj);
        alert(jsonstr);
        var newjobj=JSON.parse(jsonstr);
        alert(newjobj.email);
    }

Thanks for everyones help. I just didn't have a .value after my email. Cleared alot of other stupid mistakes aswell, but still my second alert only says [object] [object].

9
  • 2
    Any errors in your JavaScript console? If you isolated it down to one line, why did you show us the rest of the code? Commented Aug 21, 2012 at 19:37
  • 1
    could you post your html code? Commented Aug 21, 2012 at 19:39
  • 3
    any reason why not matter what gender you select... you are always male? ;-) Commented Aug 21, 2012 at 19:40
  • I'm not sure which part isn't working? stringify and alerting the string? or parsing and alerting the email? What exactly isn't working as you expected? Commented Aug 21, 2012 at 19:43
  • 1
    @Shmiddty: just to demonstrate: jsfiddle.net/Smswx Commented Aug 21, 2012 at 19:49

2 Answers 2

1

Please try this:

function createJSON()
{
    var g = "unknown",
        jsonobj,
        jsonstr,
        newjobj;

    if (document.forms["formIn"]["gender"][0].checked == true)
    {
        g = "male";
    }
    else if (document.forms["formIn"]["gender"][1].checked == true)
    {
        g = "female";
    }
    jsonobj={"name":document.forms["formIn"]["name"].value, "surname":document.forms["formIn"]["surname"].value, "email":document.forms["formIn"]["email"], "dob":document.forms["formIn"]["dob"].value, "password":document.forms["formIn"]["password"].value, "cpassword":document.forms["formIn"]["confirm_password"].value, "gender":g}
    jsonstr=JSON.stringify(jsonobj);
    alert(jsonstr);
    newjobj=JSON.parse(jsonstr);
    alert(newjobj.email);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are missing a ; at the end of where you instantiate your JSON object. Everything else seems to be okay though.

2 Comments

Semicolons are usually optional in JavaScript.
Hmm that's true... I would also like to see the error message he gets in the console as Matt Ball pointed out so if the OP could post that, it'd be a lot more helpful.

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.