0

I want to create a google.Visualization.DataTablein the end, to show a graph. By now, I have a Problem with the following:

This is my code for getting Object from JSON-string and listing Properties:

var jsonData = <?php echo "'". $jsonTable. "'"; ?>;
var parsed = JSON.parse(jsonData);

var sensors = [];

for (var x in parsed){
    sensors.push(parsed[x]);
}
var text ="";
for (var sensor in sensors){
    if (sensors.hasOwnProperty(sensor)){
        var measures = sensors[sensor];
        text += ('\r\n' + sensor);
        for (var time in measures){
            if(measures.hasOwnProperty(time)){
                text += ('\r\n' + time + " = " + measures[time]);
            }
        }
    }
}

$(document.getElementById('chart_div')).text(text);

And my jsonData looks like this:

    jsonData = '{"sensor1":
                  {"Date(2016,1,08,10,30,03)":19.187,
                   "Date(2016,1,08,10,00,02)":18.937[,...]},
                 "sensor2":
                  {"Date(2016,1,08,10,30,04)":18.687,
                   "Date(2016,1,08,10,00,03)":18.437[,...]}
                [,...]}'

My Problem is that i don't get the values "sensor1", "sensor2" and so on in the loop. text += ('\r\n' + sensor); only returns the index of the sensor-object in the sensors-object.

How can I get the sensor name instead of the index?

5
  • The string literal you are trying to assign to jsonData has a syntax error in it which will abort the script so none of your loops with every run. Commented Jan 8, 2016 at 11:09
  • @Quentin: I don't see the problem, i copy-pasted from running code. What do you mean? i only shorted it a bit for readability. So you could ignore [,...] Commented Jan 8, 2016 at 11:10
  • You can't have literal line breaks in a string literal in JS. Commented Jan 8, 2016 at 11:10
  • You should just use var parsed = <?php echo $jsonTable ?>; Commented Jan 8, 2016 at 11:58
  • Since JSON format is a subset of Javascript literal syntax. Commented Jan 8, 2016 at 11:59

2 Answers 2

1

One simple workaround

Remove the var sensors = [];

Find sensors and replace with parsed.

Code:

for (var sensor in parsed){
if (parsed.hasOwnProperty(sensor)){
    var measures = parsed[sensor];
    text += ('\r\n' + sensor);
    console.log(parsed);
    for (var time in measures){
        if(measures.hasOwnProperty(time)){
            text += ('\r\n' + time + " = " + measures[time]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

that's exactly what solved the problem after @Quentin giving me his answer.
1

How can I get the sensor name instead of the index?

You need to do something with the property name in your first loop.

At present you are taking the property name (sensor1), using it to get the value ({"Date...) and then putting the value in an array while discarding the property name.

The simplest option would be to get rid of your first loop entirely and work with parsed instead of sensors in your second loop.

1 Comment

thats the hint i needed. i 'don't know why i tried to make an array from an object. I solved it by simply using the parsed object for my loops

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.