简体   繁体   English

如何指定 JSON 数据

[英]How to specify JSON data

I'm wondering how I'm supposed to go about getting a single stream of data or rather, I'm trying to get the value that is given near the end of the script, specifically the text value for ModbusSerial.Port_1.COM.Power and I'm wondering how I might go about doing that.我想知道我应该如何 go 关于获取单个 stream 数据,或者更确切地说,我试图获取脚本末尾附近给出的值,特别是 ModbusSerial.Port_1.Z7BDFD5C1274ED7637 的文本值。电源,我想知道我怎么可能 go 这样做。 I don't know exactly how to write that code.我不确切知道如何编写该代码。 I know it's something along the lines of我知道这有点像

json.dumps(data12_json['MTConnectStreams']['Streams']['ComponentStream'], indent=4) 

but after that, I'm lost as how to further specify the path and get back my value.但在那之后,我迷失了如何进一步指定路径并取回我的价值。 Help would be greatly appreciated.帮助将不胜感激。

>>> print(json.dumps(data12_json, indent=4))
{
    "MTConnectStreams": {
        "@xmlns": "urn:mtconnect.com:MTConnectStreams:1.1",
        "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "@xsi:schemaLocation": "urn:mtconnect.com:MTConnectStreams:1.1 http://www.mtconnect.org/schemas/MTConnectStreams_1.1.xsd",
        "Header": {
            "@creationTime": "2022-09-01T22:52:13",
            "@sender": "RevPi",
            "@instanceId": "637976619262601870",
            "@bufferSize": "10000",
            "@version": "4.0.22178.0",
            "@nextSequence": "190606",
            "@firstSequence": "79",
            "@lastSequence": "190606"
        },
        "Streams": {
            "DeviceStream": {
                "@uuid": "ModbusSerial",
                "@id": "ModbusSerial",
                "@name": "ModbusSerial",
                "Events": {
                    "Other": [
                        {
                            "@dataItemId": "ModbusSerial.Faulted",
                            "@sequence": "354",
                            "@timestamp": "2022-09-01T16:45:21",
                            "@name": "Faulted",
                            "#text": "False"
                        },
                        {
                            "@dataItemId": "ModbusSerial.FaultInfo",
                            "@sequence": "79",
                            "@timestamp": "2022-09-01T16:45:20",
                            "@name": "FaultInfo"
                        }
                    ]
                },
                "ComponentStream": [
                    {
                        "@component": "Component",
                        "@componentId": "ModbusSerial.Port_1",
                        "@name": "Port_1",
                        "Samples": {
                            "Other": [
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.BaudRate",
                                    "@sequence": "359",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "BaudRate",
                                    "#text": "19200"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.SamplePeriod",
                                    "@sequence": "360",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "SamplePeriod",
                                    "#text": "1000"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.BetweenModule",
                                    "@sequence": "361",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "BetweenModule",
                                    "#text": "300"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.ExceptionCounter",
                                    "@sequence": "363",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "ExceptionCounter",
                                    "#text": "0"
                                }
                            ]
                        },
                        "Events": {
                            "Other": [
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.Configured",
                                    "@sequence": "355",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "Configured",
                                    "#text": "True"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.IsConnected",
                                    "@sequence": "471",
                                    "@timestamp": "2022-09-01T16:45:23",
                                    "@name": "IsConnected",
                                    "#text": "True"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.Error",
                                    "@sequence": "357",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "Error",
                                    "#text": "False"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.ShowErrors",
                                    "@sequence": "358",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "ShowErrors",
                                    "#text": "False"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.PortName",
                                    "@sequence": "87",
                                    "@timestamp": "2022-09-01T16:45:20",
                                    "@name": "PortName",
                                    "#text": "/dev/ttyUSB0"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.ConnectionSettings",
                                    "@sequence": "469",
                                    "@timestamp": "2022-09-01T16:45:23",
                                    "@name": "ConnectionSettings",
                                    "#text": "/dev/ttyUSB0-(19200,8,None,One)"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.Status",
                                    "@sequence": "579",
                                    "@timestamp": "2022-09-01T16:45:24",
                                    "@name": "Status",
                                    "#text": "Read successful"
                                }
                            ]
                        }
                    },
                    {
                        "@component": "Component",
                        "@componentId": "ModbusSerial.Port_1.COM3",
                        "@name": "COM3",
                        "Samples": {
                            "Other": [
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.Address",
                                    "@sequence": "364",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "Address",
                                    "#text": "1"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.CommCounter",
                                    "@sequence": "190605",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "CommCounter",
                                    "#text": "7605"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.CommTime",
                                    "@sequence": "190604",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "CommTime",
                                    "#text": "124"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.CommFailedCounter",
                                    "@sequence": "370",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "CommFailedCounter",
                                    "#text": "0"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.HoldingRegister",
                                    "@sequence": "373",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "HoldingRegister",
                                    "#text": "0"
                                }
                            ]
                        },
                        "Events": {
                            "Other": [
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.Connected",
                                    "@sequence": "578",
                                    "@timestamp": "2022-09-01T16:45:24",
                                    "@name": "Connected",
                                    "#text": "True"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.CommFailed",
                                    "@sequence": "368",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "CommFailed",
                                    "#text": "False"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.CommLastTime",
                                    "@sequence": "190606",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "CommLastTime",
                                    "#text": "9/1/2022 10:52:13 PM"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.InputRegister",
                                    "@sequence": "372",
                                    "@timestamp": "2022-09-01T16:45:21",
                                    "@name": "InputRegister",
                                    "#text": "0"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.Template",
                                    "@sequence": "130",
                                    "@timestamp": "2022-09-01T16:45:20",
                                    "@name": "Template"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.Power",
                                    "@sequence": "190595",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "Power",
                                    "#text": "60.85923"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.Current_A",
                                    "@sequence": "190596",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "Current_A",
                                    "#text": "0.8454652"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.PFA",
                                    "@sequence": "190597",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "PFA",
                                    "#text": "0.595107"
                                },
                                {
                                    "@dataItemId": "ModbusSerial.Port_1.COM3.Avg_Phase_Voltage",
                                    "@sequence": "190603",
                                    "@timestamp": "2022-09-01T18:52:13",
                                    "@name": "Avg_Phase_Voltage",
                                    "#text": "120.8791"
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
}

Your structure is pretty complex you need to write all the path down to where it can be您的结构非常复杂,您需要将所有路径写到可以到达的位置

def search(data, key):
    for component in data['MTConnectStreams']['Streams']['DeviceStream']['ComponentStream']:
        for sample in component['Samples']['Other']:
            if sample['@dataItemId'] == key:
                return sample['#text']
        for sample in component['Events']['Other']:
            if sample['@dataItemId'] == key:
                return sample['#text']
key = "ModbusSerial.Port_1.COM3.Power"
data12_json = json.load(open("data.json"))
print(search(data12_json, key))  # 60.85923

You'd probably want to pick first the dictionary object on the level where all event items are located (the most nested level that you can actually access).您可能希望首先在所有事件项所在的级别(您可以实际访问的最嵌套级别)上选择字典 object。

It can be seen from the object structure that the "ModbusSerial.Port_1.COM3.Power" is not a key, but a value in one of the objects in the array.从object结构可以看出,“ModbusSerial.Port_1.COM3.Power”不是key,而是数组中某个对象的value。 Same goes for all component events/streams on COM3. COM3 上的所有组件事件/流也是如此。 They are in the array located at:它们位于位于以下位置的数组中:

data["MTConnectStreams"]["Streams"]["DeviceStream"]["ComponentStream"]

Take this object and you'll have an array of objects, specifically various component streams, from which one has a field "@componentId": "ModbusSerial.Port_1.COM3" .拿这个 object 来说,你会有一个对象数组,特别是各种组件流,其中一个有一个字段"@componentId": "ModbusSerial.Port_1.COM3" A simple loop going through the array and picking the object containing that field would do the job.一个简单的循环遍历数组并选择包含该字段的 object 就可以完成这项工作。

Then, you would have an object, let's call it com3data .然后,您将拥有一个 object,我们称之为com3data It also has nested objects in it.它也有嵌套的对象。 It has a field called Events in it, and that field also has Other field in it.它有一个名为Events的字段,并且该字段中也有Other字段。 Accessing it through com3data["Events"]["Other"] you will now have the array of objects, where one of them has the field "@dataItemId": "ModbusSerial.Port_1.COM3.Power" .通过com3data["Events"]["Other"]访问它,您现在将拥有对象数组,其中一个对象具有字段"@dataItemId": "ModbusSerial.Port_1.COM3.Power"

Run a simple loop at com3data["Events"]["Other"] , and find an object that has the above mentioned field with that value, and the object you get is exactly what you're looking for.com3data["Events"]["Other"]处运行一个简单的循环,并找到一个具有上述字段值的 object,而您得到的 object 正是您正在寻找的。

Hope I helped, at least a little bit.希望我能帮上忙,至少一点点。

I think one area where you could be having problems is that the data is partially in list form inside the json as shown by the [] brackets instead of {}.我认为您可能遇到问题的一个领域是,数据部分是 json 内的列表形式,如 [] 括号而不是 {} 所示。 This means that to acces for example the ModbusSerial.Port_1.COM.Power entry you would need to specify the following:这意味着要访问例如 ModbusSerial.Port_1.COM.Power 条目,您需要指定以下内容:

data12_json['MTConnectStreams']['Streams']['ComponentStream'][1]["Events"]["Other"][5]["@dataItemID"]

where the indices [1] and [5] specify the entry within your list where you expect the data to be.其中索引 [1] 和 [5] 指定列表中您期望数据所在的条目。

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

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