How to deserializing a JSON into a JavaScript object

0 votes

I have a string in a Java server application that is accessed using AJAX. It looks something like the following:

var json = [{
    "adjacencies": [
        {
          "nodeTo": "graphnode2",
          "nodeFrom": "graphnode1",
          "data": {
            "$color": "#557EAA"
          }
        }
    ],
    "data": {
      "$color": "#EBB056",
      "$type": "triangle",
      "$dim": 9
    },
    "id": "graphnode1",
    "name": "graphnode1"
},{
    "adjacencies": [],
    "data": {
      "$color": "#EBB056",
      "$type": "triangle",
      "$dim": 9
    },
    "id": "graphnode2",
    "name": "graphnode2"
}];

When the string gets pulled from the server, is there an easy way to turn this into a living JavaScript object (or array)? Or do I have to manually split the string and build my object manually?

Sep 3, 2020 in Java-Script by kartik
• 37,520 points
764 views

1 answer to this question.

0 votes

Hello @kartik,

To collect all item of an array and return a json object

collectData: function (arrayElements) {

        var main = [];

        for (var i = 0; i < arrayElements.length; i++) {
            var data = {};
            this.e = arrayElements[i];            
            data.text = arrayElements[i].text;
            data.val = arrayElements[i].value;
            main[i] = data;
        }
        return main;
    },

TO parse the same data we go through like this

dummyParse: function (json) {       
        var o = JSON.parse(json); //conerted the string into JSON object        
        $.each(o, function () {
            inner = this;
            $.each(inner, function (index) {
                alert(this.text)
            });
        });

}

Hope it helps!!
Thank you!!

answered Sep 3, 2020 by Niroj
• 82,880 points

Related Questions In Java-Script

0 votes
1 answer

How to convert a ruby hash object to JSON?

Hello @kartik, You can also use JSON.generate: require 'json' JSON.generate({ foo: ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,880 points
12,632 views
0 votes
1 answer

How to get the size of a JavaScript object?

Hello @kartik, Use this code: function roughSizeOfObject( object ) ...READ MORE

answered Sep 23, 2020 in Java-Script by Niroj
• 82,880 points
4,441 views
0 votes
1 answer

How to to get the key of a key/value javascript object?

Hello @kartik, You would iterate inside the object ...READ MORE

answered Oct 9, 2020 in Java-Script by Niroj
• 82,880 points
927 views
0 votes
0 answers

How to sum the values of a javascript object?

I'm currently facing an issue while trying ...READ MORE

Sep 25, 2023 in Java-Script by Tanishqa
• 1,170 points
276 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
22,960 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,969 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,900 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
44,400 views
0 votes
1 answer

How to clone a JavaScript object?

Hello, You can clone an object and remove ...READ MORE

answered Apr 2, 2020 in Java-Script by Niroj
• 82,880 points
913 views
0 votes
1 answer

How to list the properties of a JavaScript object?

Hii @kartik, Use Reflect.ownKeys(): var obj = {a: 1, b: ...READ MORE

answered Jun 8, 2020 in Java-Script by Niroj
• 82,880 points
1,002 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP