Dallas
Dallas

Reputation: 36

making JSON from string

I have a job to refractor strings to start using json so they can just pass json objects. So I have made array of names and then I'm trying to go through and make key and values but I'm getting an error in the console that it cant find x of no value. Can someone point me in the right direction?

 var newName = ['ManagingOrg', 'ActiveOrg', 'Severity', 'SeverityClassification', 'WorkQueue', 'TicketState',................ to long to post];


            $().each(newName, function (key, value) {
                key = newName[this];
                value = newValues[this] = $('#' + key).val();
                newArray = [key][value];
                newArray = JSON.stringify(newArray);
                alert(newArray);
                $('.results').html(origArray[TicketNumber]);

            });

Upvotes: 0

Views: 74

Answers (2)

Dallas
Dallas

Reputation: 36

var origArray = [];

    var newName = ['ManagingOrg', 'ActiveOrg', 'Severity', 'SeverityClassification'
    ];


    for (var i = 0; i < newName.length - 1; i++) {
        var object = {};
        object[newName[i]] = newName[i];
        object = JSON.stringify(object);
        origArray.push(object);

    }

Upvotes: 0

S. Thrall
S. Thrall

Reputation: 19

I'm assuming you have "newValues" and "origArray" defined elsewhere?

In any case you'll need to at least adjust the following:

  • "$().each" should be $.each
  • "newArray" should be defined outside and you should use newArray[key] = value
  • you don't have a variable "TicketNumber" defined and so you should wrap "TicketNumber" in quotes
  • this is a reserved word so you shouldn't use it in "newName[this]" or "newValues[this]"

I suggest using a for loop instead of $.each() based on what you're trying to do inside.

https://msdn.microsoft.com/en-us/library/bb299886.aspx

Upvotes: 1

Related Questions