2

In my javascript I am trying to read values from a JSON file from a server. The file contains a array of objects but I cannot access the values in these objects. When I try this my log says the value is undefined. However, when I try to stringify a Object in the array it works perfectly fine. What am I doing wrong?

The JSON file:

{
  "NETWORKS": [
    {
      "NETWORK": {
        "STARTDATE": "00:13:33:15:10:2015",
        "SSID": "Ziggo323_AC",
        "WPA": "YES",
        "WEP": "NO",
        "WPS": "YES",
        "OPEN": "NO",
        "WPATEST": {
          "DURATION": "3000",
          "DATE": "00:11:26:24:09:2015",
          "ATTEMPTS": "594",
          "STATUS": "FAILED"
        },
        "WPSTEST": {
          "DURATION": "2932",
          "DATE": "03:11:28:24:09:2015",
          "ATTEMPTS": "9",
          "STATUS": "PASSED"
        }
      }
    },
    {
      "NETWORK1": {
        "STARTDATE": "00:15:26:15:10:2015",
        "SSID": "FreeWiFi",
        "WPA": "NO",
        "WEP": "NO",
        "WPS": "NO",
        "OPEN": "YES"
      }
    }
  ]
}

The javascript function

function LoadTestResults() {
  $.ajax({
    type: 'POST',
    url: 'GetJSON.php',
    data: "TestResults.json",
    dataType: "json",
    success: function (data) {
      var wpsDuration = data.NETWORKS[0];
      console.log(JSON.stringify(wpsDuration)); // output ={"NETWORK":{"STARTDATE":"00:13:33:15:10:2015" etc.
      console.log(wpsDuration.WPA); // output = undefined
    }
  });
}
1
  • i think you need to access it like console.log(wpsDuration.NETWORK.WPA) instead of console.log(wpsDuration.WPA); Commented Oct 16, 2015 at 7:54

3 Answers 3

2

Just try with:

wpsDuration.NETWORK.WPA

or with a loop:

for (var k in data.NETWORKS) {
  var network = data.NETWORKS[k];
  var networkKey = Object.keys(network).pop();

  console.log(networkKey, network[networkKey].WPA);
}
Sign up to request clarification or add additional context in comments.

7 Comments

That works thank you. But I want the function to be dynamical. If there are many networks (like network1, network 2) I want to be able to access them like NETWORKS[x]. Is there a way to do this?
Loop through them and give them out for each.
So you have to loop this wpsDuration object. Check my edit.
I have tried but it only gives me the value of the NETWORK and not NETWORK1. The console gives the output: NETWORK YES
@RikSmits You're right - I see the problem. Check my answer now.
|
0

Put a debugger; before this line var wpsDuration = data.NETWORKS[0]; and inspect the data.

Keep your console open while debugging. Once the success will reach debugger will hit.

then move to console window and try different combinations. This will help you to understand your object.

Comments

0

I tried and it worked for me

Put a debugger; before this line var wpsDuration = data.NETWORKS[0]; and inspect the data

1 Comment

Could you please elaborate more your answer adding a little more description about the solution you provide?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.