Home > database >  Cannot get json to loop properly in javascript
Cannot get json to loop properly in javascript

Time:12-31

I cannot figure out how to loop though this JSON return properly. It seems formatted properly but using JSON.Parse() just isnt working for me. It does work for the first two $POST variables that are returned and I can use them without issue. Im wondering if im having so many issues because I'm getting this JSON return from the echo'd JSON array that im echoing out back to the ajax. Whats strange to me is that ive saved this to a local.json file and I can parse it without issue.

{
    "0": {
      "Name": "QG",
      "Guid": "3e2e4830-7954-4248-b580-03aa04f3add8",
      "TypeName": "PresentationFolder",
      "LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
      "TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
      "ParentFolderName": "HELLO_WORLD",
      "ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
      "Attributes": "Normal",
      "Enabled": "false",
      "IsSchedulableItem": "false",
      "ScheduleEnabled": "false",
      "SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
    },
    "1": {
      "Name": "OR",
      "Guid": "51f61b92-2b88-4820-a3a4-042c79844e5d",
      "TypeName": "PresentationFolder",
      "LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
      "TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
      "ParentFolderName": "HELLO_WORLD",
      "ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
      "Attributes": "Normal",
      "Enabled": "false",
      "IsSchedulableItem": "false",
      "ScheduleEnabled": "false",
      "SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
    },
    "2": {
      "Name": "4O",
      "Guid": "90ccb0c9-ab7a-4d3e-94d0-09a9983bbce7",
      "TypeName": "PresentationFolder",
      "LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
      "TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
      "ParentFolderName": "HELLO_WORLD",
      "ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
      "Attributes": "Normal",
      "Enabled": "false",
      "IsSchedulableItem": "false",
      "ScheduleEnabled": "false",
      "SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
    },
    "3": {
      "Name": "VV",
      "Guid": "16047f72-2ee0-4cc0-acd8-0c19e9a2694c",
      "TypeName": "PresentationFolder",
      "LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
      "TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
      "ParentFolderName": "HELLO_WORLD",
      "ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
      "Attributes": "Normal",
      "Enabled": "false",
      "IsSchedulableItem": "false",
      "ScheduleEnabled": "false",
      "SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
    },
    "4": {
      "Name": "5F",
      "Guid": "4e7bcd95-44fd-4d10-a586-14a5e77f6e22",
      "TypeName": "PresentationFolder",
      "LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
      "TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
      "ParentFolderName": "HELLO_WORLD",
      "ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
      "Attributes": "Normal",
      "Enabled": "false",
      "IsSchedulableItem": "false",
      "ScheduleEnabled": "false",
      "SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
    },
    "5": {
      "Name": "FR",
      "Guid": "1ca5a5ce-ac5c-4fd7-8f10-14a8585d4de9",
      "TypeName": "PresentationFolder",
      "LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
      "TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
      "ParentFolderName": "HELLO_WORLD",
      "ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
      "Attributes": "Normal",
      "Enabled": "false",
      "IsSchedulableItem": "false",
      "ScheduleEnabled": "false",
      "SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
    },
    "6": {
      "Name": "6B",
      "Guid": "f76307dd-4500-4ace-a756-1b4b207801c2",
      "TypeName": "PresentationFolder",
      "LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
      "TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
      "ParentFolderName": "HELLO_WORLD",
      "ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
      "Attributes": "Normal",
      "Enabled": "false",
      "IsSchedulableItem": "false",
      "ScheduleEnabled": "false",
      "SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
    },
    "7": {
      "Name": "AK",
      "Guid": "3ffbb7a5-e5a4-458f-a9c7-1f8a29df58dc",
      "TypeName": "PresentationFolder",
      "LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
      "TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
      "ParentFolderName": "HELLO_WORLD",
      "ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
      "Attributes": "Normal",
      "Enabled": "false",
      "IsSchedulableItem": "false",
      "ScheduleEnabled": "false",
      "SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
    },
    "8": {
      "Name": "MK",
      "Guid": "e51e3daa-d10f-43ce-b3ce-202bb9e5f227",
      "TypeName": "PresentationFolder",
      "LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
      "TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
      "ParentFolderName": "HELLO_WORLD",
      "ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
      "Attributes": "Normal",
      "Enabled": "false",
      "IsSchedulableItem": "false",
      "ScheduleEnabled": "false",
      "SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
    },
    "9": {
      "Name": "VH",
      "Guid": "c332aa7b-0a3a-4b24-920a-20e4273c7902",
      "TypeName": "PresentationFolder",
      "LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
      "TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
      "ParentFolderName": "HELLO_WORLD",
      "ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
      "Attributes": "Normal",
      "Enabled": "false",
      "IsSchedulableItem": "false",
      "ScheduleEnabled": "false",
      "SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
    },
    "10": {
      "Name": "3K",
      "Guid": "d33da5b4-29cf-401f-8880-27d773d56d2b",
      "TypeName": "PresentationFolder",
      "LongTypeName": "Altiris.NS.ItemManagement.PresentationFolder",
      "TypeGuid": "f1e8956a-a158-487f-8185-4c3b492734e7",
      "ParentFolderName": "HELLO_WORLD",
      "ParentFolderGuid": "e3ceea75-510e-4900-bb5f-6b0d47309bd3",
      "Attributes": "Normal",
      "Enabled": "false",
      "IsSchedulableItem": "false",
      "ScheduleEnabled": "false",
      "SharedScheduleGuid": "00000000-0000-0000-0000-000000000000"
    }

UPDATE: I was finally able to to get better results and get the JSON displaying correctly. By setting my variable to var json = data.body[0]; and outputting like this $('.modal-body').html('<pre>' JSON.stringify(json, null, 2) '</pre>'); I just cant figure out this .each() statement now to iterate though all of the objects I only get the last value at the bottom of this JSON.

$.each(json, function() {
    var Name = this.Name;
    var Guid = this.Guid;
    var TypeName = this.TypeName;
    var LongTypeName = this.LongTypeName;
    var TypeGuid = this.TypeGuid;
    var ParentFolderName = this.ParentFolderName;
    var ParentFolderGuid = this.ParentFolderGuid;
    var Attributes = this.Attributes;
    var Enabled = this.Enabled;
    var IsSchedulableItem = this.IsSchedulableItem;
    var ScheduleEnabled = this.ScheduleEnabled;
    var SharedScheduleGuid = this.SharedScheduleGuid;

    // $('.modal-body').html(Name   " "   Guid   " "   TypeName   " "   LongTypeName   " "   TypeGuid   " "   ParentFolderName   " "   ParentFolderGuid   " "   Attributes   " "   Enabled   " "   IsSchedulableItem   " "   ScheduleEnabled   " "   SharedScheduleGuid);
    $('.modal-body').html(`${name}   " "   ${value}`);
});

CodePudding user response:

The issue is that you are setting the inner HTML value for the .modal-body element on each iteration of your $.each loop. This means that, upon completion of the loop, the inner HTML of .modal-body will be the data corresponding to only the last item in your parsed JSON.

The approach I would take to this problem is to use $.map instead of $.each. I would map the first-level properties of the parsed JSON into the values I want to inject into the HTML of .modal-body. I would then join these array elements with a suitable delimiter, which will produce a string I can insert into .modal-body.html().

The result would look something like:

const mappedItems = $.map(json, function(item) {
  return `${item.Name}`;
});

$('.modal-body').html(mappedItems.join('<br>'));

Here is a fiddle for reference.

CodePudding user response:

Updated Code to below and its working I should of just taken a break and looked with fresh eyes!! ->

for (obj in json) {
    if (typeof json[obj] === 'string') {

        console.log(`${obj}=>${json[obj]}`);
        var div = document.getElementById('modal-body');
        div.innerHTML  = (`${obj}=>${json[obj]}`   "<br>");
    }
  }
}
  • Related