简体   繁体   English

如何在不知道键的情况下使用javascript从多级JSON获取值

[英]How to get values from multi-level JSON using javascript without knowing the keys

I have the following json: 我有以下json:

{"sensors": 
        {"-KqYN_VeXCh8CZQFRusI": 
            {"bathroom_temp": 16,
             "date": "02/08/2017", 
             "fridge_level": 8,
             "kitchen_temp": 18, 
             "living_temp": 17, 
             "power_bathroom": 0, 
             "power_bathroom_value": 0, 
             "power_kit_0": 0
        }, 
        "-KqYPPffaTpft7B72Ow9": 
            {"bathroom_temp": 20, 
             "date": "02/08/2017", 
             "fridge_level": 19, 
             "kitchen_temp": 14, 
             "living_temp": 20, 
             "power_bathroom": 0, 
             "power_bathroom_value": 0
        },  
        "-KqYPUld3AOve8hnpnOy": 
            {"bathroom_temp": 23, 
             "date": "02/08/2017", 
             "fridge_level": 40, 
             "kitchen_temp": 11, 
             "living_temp": 10, 
             "power_bathroom": 1, 
             "power_bathroom_value": 81, 
        }
    }
}

I am reading it from a file using the following python file : 我正在使用以下python文件从文件中读取它:

from flask import Flask, render_template, json, url_for
from firebase import firebase
import os




firebase = firebase.FirebaseApplication('https://my-firebase-db-958b1.firebaseio.com/', None)
result = firebase.get('/Dublin-Ireland', None)
print "\n Json file created!"


with open ('data.txt', 'w') as outfile:
    json.dump(result, outfile)


app = Flask(__name__)

@app.route('/')
def homepage():
    with open('data.txt') as f:
        s = f.read()

    return render_template("homepage.html" , nameTxt = s)


if __name__ == "__main__":
    app.run()

I can pass the json data from the python file to the javascript. 我可以将json数据从python文件传递到javascript。 After parsing, i'm able to see all the values using JSON.Stringify. 解析之后,我可以使用JSON.Stringify查看所有值。 But, i'm not able to get one value at a time. 但是,我一次无法获得一个价值。 What i'm trying to do is to save each key in a file of its own(eg. bathroom_temp, date, fridge_level, etc...) and append the values as the json gets updated in a periodic manner. 我想做的是将每个键保存在其自己的文件中(例如,bathroom_temp,date,Refrigerator_level等),并在json定期更新时附加值。 So that, later on i can just get the data of whatever key i need and plot a line graph using the data. 这样,以后我就可以获取我需要的任何键的数据,并使用该数据绘制折线图。 For example, the bathroom_temp file should contain "16, 20, 23", and this can be used to plot a graph. 例如,bathroom_temp文件应包含“ 16、20、23”,并且可以用来绘制图形。

The following is my javascript code: 以下是我的JavaScript代码:

<script>
      var test = JSON.parse({{ nameTxt|tojson }});
      for(key in test)
      {
          if(test.hasOwnProperty(key))
          {
              var value = test[key];
              alert(JSON.stringify(value, null, 4));

           }
       }
  </script>

You can generalize something like: 您可以概括如下:

for(key in test['sensors']) {
  var sensor = test['sensors'][key];
  console.log(sensor['bathroom_temp']);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM