Home JSON vs. JavaScript object array. Why is the second approach different in result than the first? And how to achieve this?
Reply: 0

JSON vs. JavaScript object array. Why is the second approach different in result than the first? And how to achieve this?

user3152
1#
user3152 Published in April 26, 2018, 1:46 pm

I know this is NOT a valid JSON, but when I declare this as variable directly within my code, then I can use it as an object.

<script>
//  this will result in object
    var mydata = {
        users: [{
            person: {
                firstName: "Garry",
                lastName: "Finch"
            },
            jobTitle: "Front End Technical Lead",
            twitter: "gazraa"
        }, {
            person: {
                firstName: "Hans",
                lastName: "Wurst"
            },
            jobTitle: "Photographer",
            twitter: "photobasics"
        }, {
            person: {
                firstName: "Paul",
                lastName: "Stark"
            },
            jobTitle: "LEGO Geek",
            twitter: "minifigures"
        }]
    };

    console.log('mydata: ' + mydata);
    console.log(mydata);
</script>

now I would like to store the same information into a file "table.data.js". the file then looks like: (the inner of BEGIN and END)

//BEGIN
{
    users: [{
        person: {
            firstName: "Garry",
            lastName: "Finch"
        },
        jobTitle: "Front End Technical Lead",
        twitter: "gazraa"
    }, {
        person: {
            firstName: "Hans",
            lastName: "Wurst"
        },
        jobTitle: "Photographer",
        twitter: "photobasics"
    }, {
        person: {
            firstName: "Paul",
            lastName: "Stark"
        },
        jobTitle: "LEGO Geek",
        twitter: "minifigures"
    }]
}
//END

then I have tried different ways to read this file

$.ajax({
    contentType: "application/json; charset=utf-8",
    url: "Content/modules/Controls/table/table.data.js",
    data: "{}",
    dataType: "json",
    success: function(data) {
        var json = data;
        console.log(json);
    }
});

then tried with $.get and $.getScript in any case the result would be not the same object

//var jqxhr = $.get("Content/modules/Controls/table/table.data.js", function () {
var jqxhr = $.getScript("Content/modules/Controls/table/table.data.js", function (data, textStatus, jqxhr) {
        console.log("success");
    }).done(function (data) {
        console.log(data);
        testdata = JSON.stringify(data);
        console.log(testdata);
  }).fail(function () {
      console.log("error");
  }).always(function () {
      console.log("finished");
  });

The result would never be the same object. I am only able to log the data as "plain" text into console. I guess this is a small step for some with knowledge, but actually for me a book with seven seals.

EDIT: Thanks for all the responses. In summary that means, it is NOT possible to load the external data, store it in a variable like "mydata" and then work the same way as declaring it directly within the code? I will change the data to a valid JSON then.

Then I would give @Quentin the medal for the shortest precise answer. But,thanks all for your clarification.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308044 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO