1

I know this question has been asked quite a lot before, but I spent more than 4 hours trying to figure out what is the problem but I didn't have any luck to find a similar case. I have complex javascript object that I need to convert it to Json using Json.stringfy() method, but the problem is I'm always getting empty Object. Here is a screenshot for the object, and this is the result I'm getting after calling json.stringfy().

Here is the code for creation the object:

function Step2() {
    debugger;
    //Get all table rows
    var tableRows = $('#ProjectsTable > tbody  > tr');

    //Remove first two rows
    tableRows.splice(0, 2);
    var tds = []
    tableRows.each(function () {

        tds.push($(this).find('td'));

    })
    var self = new Object();
    self.Projects = new Array();
    for (var i = 0; i < tds.length; i++) {

        self.Projects.push([]);
        self.Projects[i].Years = new Array();
        self.Projects[i].ProjectName = "";


        self.Projects[i].ProjectName = $(tds[i][0]).find('span').text();
        for (var j = 1; j < tds[i].length;) {

            var Year = new Object();
            Year.TimeAllocated = $(tds[i][j++]).text();
            Year.Duration = $(tds[i][j++]).text();
            Year.Cost = $(tds[i][j++]).text();
            self.Projects[i].Years.push(Year);

        }
    }
    return self;
}


function SubmitStep2() {
    debugger;
    var request =  Step2();
    var jsonData = JSON.stringify(request);
    debugger;
    $.ajax({
        url: "/ResearchGrant/ProjectManpower",
        dataType: 'json',
        contentType: "application/json",
        type: "POST",
        data: jsonData ,
    })
    .success(function (result) {
        ShowSuccessMessage();
    })
    .error(function (xhr, status) {
        alert(status + xhr.error);
    })
}
4
  • 4
    But...your not calling json.stringfy() anywhere in the code above?! If you create a Minimal, Complete, and Verifiable example people may be able to help, right now you're just asking us to guess Commented Feb 23, 2016 at 17:26
  • 1
    Also, please stop using screenshots. 90% of the time they're totally unhelpful. Post code and details of recreation steps, screenshots just show results, not how you got there Commented Feb 23, 2016 at 17:31
  • I feel like your are using stringify before the projects are added to your object. Is your code asynchrone? Commented Feb 23, 2016 at 17:35
  • @Liam I tried to add screenshots using Imgur embedded in stackoverflow but It won't work sorry for that Commented Feb 23, 2016 at 17:41

2 Answers 2

3

One problem is here:

    self.Projects.push([]);
    self.Projects[i].Years = new Array();

In this case, self.projects[i] is an array, which means it doesn't (can't) have an attribute called Years. Instead, push a new Object:

    self.Projects.push({});
    self.Projects[i].Years = new Array();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir for your response and your very simple and self contained answer. World with people like you is much nicer place, thanks again sir
0

In my case:

I have initialised as array but treated that as object.

That resulted in empty object after JSON.stringify.

let sample: any = [];
Object.assign(sample, {item : "a"});
let stringfied = JSON.stringify(sample);

Worked out after correcting the type.

let sample: any = {};

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.