在python中将json数据转换为变量的问题

在python中将json数据转换为变量的问题,python,domoticz,Python,Domoticz,我一直在从事一个教育项目,其中一小部分需要我将一行json数据转换成python 3中的一个变量,这是我从domoticz(一个外部开放源代码软件)收到的。然而,由于我对json的技能水平,我经历了一些问题,我不确定我到底做错了什么。我确实每次都收到200条响应,所以我根据我的理解认为这意味着连接不是问题,而是python代码。(我审查了地址,但它们是正确的。) 通过在python中键入并单击url,我可以看到返回的json: { "ActTime" : 160672

我一直在从事一个教育项目,其中一小部分需要我将一行json数据转换成python 3中的一个变量,这是我从domoticz(一个外部开放源代码软件)收到的。然而,由于我对json的技能水平,我经历了一些问题,我不确定我到底做错了什么。我确实每次都收到200条响应,所以我根据我的理解认为这意味着连接不是问题,而是python代码。(我审查了地址,但它们是正确的。)

通过在python中键入并单击url,我可以看到返回的json:

{
    "ActTime" : 1606722346,
    "AstrTwilightEnd" : "18:37",
    "AstrTwilightStart" : "06:23",
    "CivTwilightEnd" : "17:14",
    "CivTwilightStart" : "07:47",
    "DayLength" : "08:08",
    "NautTwilightEnd" : "17:56",
    "NautTwilightStart" : "07:04",
    "ServerTime" : "2020-11-30 08:45:46",
    "SunAtSouth" : "12:30",
    "Sunrise" : "08:26",
    "Sunset" : "16:34",
    "app_version" : "2020.2",
    "result" : 
    [
        {
            "AddjMulti" : 1.0,
            "AddjMulti2" : 1.0,
            "AddjValue" : 0.0,
            "AddjValue2" : 0.0,
            "BatteryLevel" : 255,
            "CustomImage" : 0,
            "Data" : "Normal",
            "Description" : "",
            "Favorite" : 0,
            "HardwareID" : 1,
            "HardwareName" : "Domoticz Internal",
            "HardwareType" : "Domoticz Internal interface",
            "HardwareTypeVal" : 67,
            "HaveDimmer" : false,
            "HaveGroupCmd" : false,
            "HaveTimeout" : false,
            "ID" : "148702",
            "LastUpdate" : "2020-10-19 15:10:02",
            "MaxDimLevel" : 0,
            "Name" : "Domoticz Security Panel",
            "Notifications" : "false",
            "PlanID" : "0",
            "PlanIDs" : 
            [
                0
            ],
            "Protected" : false,
            "ShowNotifications" : true,
            "SignalLevel" : "-",
            "Status" : "Normal",
            "StrParam1" : "",
            "StrParam2" : "",
            "SubType" : "Security Panel",
            "SwitchType" : "Security",
            "SwitchTypeVal" : 0,
            "Timers" : "false",
            "Type" : "Security",
            "TypeImg" : "security",
            "Unit" : 0,
            "Used" : 0,
            "XOffset" : "0",
            "YOffset" : "0",
            "idx" : "2"
        },
        {
            "AddjMulti" : 1.0,
            "AddjMulti2" : 1.0,
            "AddjValue" : 0.0,
            "AddjValue2" : 0.0,
            "BatteryLevel" : 255,
            "CustomImage" : 0,
            "Data" : "-5.0 C",
            "Description" : "",
            "Favorite" : 1,
            "HardwareID" : 2,
            "HardwareName" : "Test sensor",
            "HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
            "HardwareTypeVal" : 15,
            "HaveTimeout" : true,
            "ID" : "14053",
            "LastUpdate" : "2020-11-09 09:03:34",
            "Name" : "Temperatuur Kachel",
            "Notifications" : "false",
            "PlanID" : "0",
            "PlanIDs" : 
            [
                0
            ],
            "Protected" : false,
            "ShowNotifications" : true,
            "SignalLevel" : "-",
            "SubType" : "LaCrosse TX3",
            "Temp" : -5.0,
            "Timers" : "false",
            "Type" : "Temp",
            "TypeImg" : "temperature",
            "Unit" : 1,
            "Used" : 1,
            "XOffset" : "0",
            "YOffset" : "0",
            "idx" : "3",
            "trend" : 0
        },
        {
            "AddjMulti" : 1.0,
            "AddjMulti2" : 1.0,
            "AddjValue" : 0.0,
            "AddjValue2" : 0.0,
            "BatteryLevel" : 255,
            "CustomImage" : 0,
            "Data" : "17.5",
            "Description" : "",
            "Favorite" : 1,
            "HardwareID" : 3,
            "HardwareName" : "Test switch",
            "HardwareType" : "Dummy (Does nothing, use for virtual switches only)",
            "HardwareTypeVal" : 15,
            "HaveTimeout" : true,
            "ID" : "0014054",
            "LastUpdate" : "2020-11-06 11:51:09",
            "Name" : "Temperatuur gewenst",
            "Notifications" : "false",
            "PlanID" : "0",
            "PlanIDs" : 
            [
                0
            ],
            "Protected" : false,
            "SetPoint" : "17.5",
            "ShowNotifications" : true,
            "SignalLevel" : "-",
            "SubType" : "SetPoint",
            "Timers" : "false",
            "Type" : "Thermostat",
            "TypeImg" : "override_mini",
            "Unit" : 1,
            "Used" : 1,
            "XOffset" : "0",
            "YOffset" : "0",
            "idx" : "4"
        }
    ],
    "status" : "OK",
    "title" : "Devices"
}
基本上,我想要SetPoint(在文本的最后一个选项卡中,也在这个选项卡的正上方),它在本例17.5中作为python中的一个变量,我将使python代码循环,以便它每次都获取json url来更新SetPoint的状态。但是我有问题,只是为了把17.5变成一个变量。我最终得到了整个json,就像这段代码所做的那样。或者,如果我改变了一些东西,我最终会让一切都过去,包括设定点。有没有人知道我做错了什么,以及我应该在哪里寻找解决方案?我对python的json部分有点不熟悉,我不知道从哪里开始,因为我找到的代码和尝试过的代码似乎不起作用,或者给了我错误。
非常感谢您抽出时间

您正在将数据作为字典存储在
data={“key”:argument}
中。 如果要访问某个值,必须调用它。就你而言:

SetPoint = float(data["result"][-1]["SetPoint"])
要分解它:

data["result"] # gives you a list which elements are dictionaries.
the [-1] # calls for the last element in you list which contains the SetPoint
["SetPoint"] # then calls for the SetPoint Value which is a String "17.5"
float(...) converts the string to a float value

loads返回一个python字典,因此类似这样的操作可能会:

result = json['result']
set_point = 0.0 

for res in result:
   if 'SetPoint' in res:
        set_point = res['SetPoint']
result = json['result']
set_point = 0.0 

for res in result:
   if 'SetPoint' in res:
        set_point = res['SetPoint']