Craig Hopson Posted August 27, 2013 Share Posted August 27, 2013 Hi guys my JS is poor at best i have this [Object { prob2=23.5}, Object { prob1=23.375}] showing in my console.log it comes from a AJAX request from PHP i'm getting this out put by this $.ajax({ url:"temps.php", dataType: "json", success:function(data) { console.log(data); } how can i use thi information like this??? $.ajax({ url:"temps.php", dataType: "json", success:function(data) { document.getElementById("prob1").innerHTML = data.prob1; } Please help cos this dont work lol Thanks Link to comment Share on other sites More sharing options...
thescientist Posted August 27, 2013 Share Posted August 27, 2013 (edited) Any errors in the console? do you have control over the backend? It seems like there's an extra layer of nesting. I would expect a flatter object being returned, something like { prop1: 23.375 prop2: 23.5} do you have a live link to this page anywhere? Edited August 27, 2013 by thescientist Link to comment Share on other sites More sharing options...
Craig Hopson Posted August 27, 2013 Author Share Posted August 27, 2013 (edited) Any errors in the console? do you have control over the backend? It seems like there's an extra layer of nesting. I would expect a flatter object being returned, something like { prop1: 23.375 prop2: 23.5} it is coming from here #!/usr/bin/pythonimport sys, json, oslist = os.listdir("/sys/bus/w1/devices/")list.remove("w1_bus_master1")# we can also build lists, first start with an empty oneoutput = []for sensor in list: # Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before. tfile = open("/sys/bus/w1/devices/" + sensor + "/w1_slave") # Read all of the text in the file. text = tfile.read() # Close the file now that the text has been read. tfile.close() # Split the text with new lines (n) and select the second line. secondline = text.split("n")[1] # Split the line into words, referring to the spaces, and select the 10th word (counting from 0). temperaturedata = secondline.split(" ")[9] # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number. temperature = float(temperaturedata[2:]) # Put the decimal point in the right place and display it. temperature = temperature / 1000 #print temperature output.append({ sensor:temperature })# Send it to stdout (to PHP)out = json.dumps(output,separators=(',', ':'))print out output for this is [{"prob2":23.75},{"prob1":23.625}] Edited August 27, 2013 by Craig Hopson Link to comment Share on other sites More sharing options...
Ingolme Posted August 27, 2013 Share Posted August 27, 2013 What you have there is an array with two elements: Object { prob2=23.5} Object { prob1=23.375} So to access the elements you would need to use data[0].prob2 and data[1].prob1 Link to comment Share on other sites More sharing options...
thescientist Posted August 28, 2013 Share Posted August 28, 2013 doh, overlooked the square brackets on the outside... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now