javascript json not working as expected

BeerFizz
BeerFizz used Ask the Experts™
on
Please see attached code snippet.

the statements:

        var JSONtoList  = JSON.stringify(message.toList); // test to see if json list
        var JSONMessage = JSON.stringify(message);

both fail to stringify the array.   Perhaps this is because it is an array of arrays?

How do I fix this please?  Code snippet please.  Thanks.
Phil
<?php

?>

<!DOCTYPE html>
<HTML>
<head>


<script language='javascript' type='text/javascript'>

var guestInput  = {};

var emails = ["phild@charter.net", "BlakeL@pacbell.net", "frank@email.net"];

guestInput.fName = "Phil";
guestInput.lName = "Davis"
guestInput.emails = emails;



    window.onload=initJobs;
    
    
    function initJobs() {
       var message = {};
       message = prepMessage();
       sendEmailMessage(message);
    }
    
    function sendEmailMessage(message) {
        var JSONtoList  = JSON.stringify(message.toList); // test to see if json list
        var JSONMessage = JSON.stringify(message);
        var dataParam = encodeURIComponent(JSONMessage);
        
        var x;
        x = 1 ;
    }


    
    
    function prepMessage() {
        var mess = {};
        var toList = [];
        var subject = "The Subject";
        var body = "The Body of the message";       
        
        var cnt = 3;
        var toList = [];
        
        var emailTarget = [];
        emailTarget['name']  = guestInput.fName + " " + guestInput.lName;
        emailTarget['email'] = guestInput.emails[0];
        toList[0] = copyEmailTarget(emailTarget);
        alert("pause");
        var eCnt = 1;
        for (var i=1; i<cnt; i++) {
            if (guestInput.emails[i] != "") {
                emailTarget['name']  = "";
                emailTarget['email'] = guestInput.emails[i];
                toList[eCnt] = copyEmailTarget(emailTarget);
                eCnt++;
            }
        }        
        
        mess.toList     = toList;
        mess.subject    = subject;
        mess.body       = body;
        
        return mess;
    }
    
    
    function copyEmailTarget(emailTarget) {
        var email = [];
        email['name']  = emailTarget['name'];
        email['email'] = emailTarget['email'];
        return email;
    }
    

    
</script>

</head>
<body>

<div  style='border:0px solid;  margin-left:0px;  '>

    <div  id='' style='border:0px solid;  margin-left:0px;  '>

    </div>
    
</div>

</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
JSON method are not supported on old browser, you need to include it
open this file, read the first lines : https://raw.github.com/douglascrockford/JSON-js/master/json2.js

Main page :
https://github.com/douglascrockford/JSON-js

Author

Commented:
Hi Leak,

json is supported on my browser.  The issue is it is failing to stringify this array??

Phil
Commented:
Change your arrays of

>>var email = [];

form to objects as

var email = {};

JSON doesnot work with string based indexing I believe.

Author

Commented:
That was in.  Thanks :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial