Currently, I am dealing with a JSON string of this specific format:
{"prey":["{\"distance\": 8.686924173343307, \"signal\": \"-59\", \"frequency\": 2447, \"mac\": \"00:00:00:00:00:00\", \"ip\": \"192.168.43.27\"}"]}
To parse this JSON, I am utilizing the following code:
var jsonSpy = JSON.parse(userList);
My goal is to extract the distance value from it using the code provided below. I have tried two different approaches which yield similar results. Although I can successfully output each set of data including distance, signal, frequency, mac, and ip individually, I am facing an issue when attempting to retrieve a particular piece of information as indicated in the code snippet. Despite that both iterations through the loop and using each function seem to produce the same outcome of displaying the JSON string, I encounter a problem specifically when trying to access the 'distance' parameter - instead of an error message, all I get is 'undefined'.
for(var i = 0; i < jsonSpy.prey.length; i++)
{
console.log(jsonSpy.prey[i]);
console.log(jsonSpy.prey[i].distance);
ctx.fillRect(jsonSpy.prey[i].distance, 300, 20, 20);
}
$.each(jsonSpy.prey, function(i, item) {
console.log(jsonSpy.prey[i]);
console.log(jsonSpy.prey[i].distance);
ctx.fillRect(jsonSpy.prey[i].distance, 300, 20, 20);
});