使用python写从文件MockServer.log读取{"MsgType": 4002, "MsgSequence": 978, "DeviceSN": "qxwz0007", "Timestamp": 1678177163410, "CoordinateSystem": 1, "PtcSize": 21, "PtcList": [ {"Id": 1233, "Timestamp": 1678177163410, "IsTracked": 2, "Type": 1, "PlateNum": "None", "Length": 490.0, "Width": 180, "Height": 145, "Lon": 120.4489336, "Lat": 31.600737, "Ele": 12.9073119, "XPos": 58.6922748, "YPos": 23.1289603, "PosConfidence": 8, "Speed": 0.0722669, "Heading": 14.3506278, "VehType": 10, "VehColor": -1, "EnterLane": -1, "ExportLane": -1, "Lane": 0, "ToStop": -1, "CrossName": ""}]}循环读取该文件中数据中所有id的值
时间: 2023-03-31 19:04:29 浏览: 167
可以使用以下代码实现:
```python
import json
with open('MockServer.log', 'r') as f:
data = f.readlines()
for line in data:
try:
json_data = json.loads(line)
if json_data['MsgType'] == 4002:
for ptc in json_data['PtcList']:
print(ptc['Id'])
except:
continue
```
这段代码会循环读取文件中的每一行数据,将其转换为 JSON 格式,并判断是否为 MsgType 为 4002 的数据。如果是,则遍历其中的 PtcList,输出每个 Ptc 的 Id 值。
相关问题
使用python写从{"MsgType": 4002, "MsgSequence": 978, "DeviceSN": "qxwz0007", "Timestamp": 1678177163410, "CoordinateSystem": 1, "PtcSize": 21, "PtcList": [ {"Id": 1233, "Timestamp": 1678177163410, "IsTracked": 2, "Type": 1, "PlateNum": "None", "Length": 490.0, "Width": 180, "Height": 145, "Lon": 120.4489336, "Lat": 31.600737, "Ele": 12.9073119, "XPos": 58.6922748, "YPos": 23.1289603, "PosConfidence": 8, "Speed": 0.0722669, "Heading": 14.3506278, "VehType": 10, "VehColor": -1, "EnterLane": -1, "ExportLane": -1, "Lane": 0, "ToStop": -1, "CrossName": ""}]}该数据中获取id的值
可以使用以下代码获取id的值:
```python
data = {"MsgType": 4002, "MsgSequence": 978, "DeviceSN": "qxwz0007", "Timestamp": 167817716341, "CoordinateSystem": 1, "PtcSize": 21, "PtcList": [{"Id": 1233, "Timestamp": 167817716341, "IsTracked": 2, "Type": 1, "PlateNum": "None", "Length": 490., "Width": 180, "Height": 145, "Lon": 120.4489336, "Lat": 31.600737, "Ele": 12.9073119, "XPos": 58.6922748, "YPos": 23.1289603, "PosConfidence": 8, "Speed": .0722669, "Heading": 14.3506278, "VehType": 10, "VehColor": -1, "EnterLane": -1, "ExportLane": -1, "Lane": , "ToStop": -1, "CrossName": ""}]}
id_value = data["PtcList"][]["Id"]
print(id_value)
```
输出结果为:
```
1233
```
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ 这个是什么问题,怎么解决
这个异常 "com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $" 是由于 JSON 数据的结构与你的代码中的类型不匹配导致的。
根据异常信息,它指出在 JSON 数据的第一行第二列处遇到了一个对象(BEGIN_OBJECT),但是你的代码期望的是一个数组(BEGIN_ARRAY)。
根据你提供的 JSON 数据示例,根节点是一个对象,而不是一个数组。因此,你需要将 `DataObject` 类中的 `data` 字段从 `List<User>` 修改为 `User` 对象。
以下是修改后的示例代码:
```java
import com.google.gson.Gson;
public class DataObject {
private int msgType;
private String msgText;
private int recordCount;
private User data;
// Getters and Setters
public static class User {
private String userID;
private String userName;
private String password;
private boolean activeFlag;
private String refreshTime;
private boolean safeModeFlag;
private boolean footManFlag;
private String validate;
private String remark;
private int lastUseSysID;
private String userNoteID;
private String loginIP;
private String userTel;
private String openID;
private String msgCode;
private String sendCodeTime;
private String userRole;
// Getters and Setters
}
public static void main(String[] args) {
String json = "{\"Msg_Type\":0,\"Msg_Text\":\"OK\",\"RecordCount\":1,\"Data\":{\"UserID\":\"001\",\"UserName\":\"Admin\",\"Password\":\"C4A084E1\",\"ActiveFlag\":true,\"RefreshTime\":\"2023/7/24 12:08:52\",\"SafeModeFlag\":false,\"FootManFlag\":false,\"Validate\":\"2119/1/1\",\"Remark\":\"管理员111\",\"LastUseSysID\":3,\"UserNoteID\":\"\",\"LoginIP\":\"127.0.0.1\",\"UserTel\":\"13502893477\",\"OpenID\":\"o-R8W5CVpplHf9QvohbxxRrzuISU\",\"MsgCode\":\"8514\",\"SendCodeTime\":\"2022/10/14 16:45:04\",\"UserRole\":\"总经理\"}}";
Gson gson = new Gson();
DataObject dataObject = gson.fromJson(json, DataObject.class);
DataObject.User user = dataObject.getData();
System.out.println("UserID: " + user.getUserID());
System.out.println("UserName: " + user.getUserName());
System.out.println("Password: " + user.getPassword());
// 获取其他属性值
}
}
```
在这个修改后的示例中,`data` 字段的类型由 `List<User>` 修改为 `User`,以便与 JSON 数据的结构匹配。现在,我们可以通过调用 `dataObject.getData()` 获取 `User` 对象,并访问其属性值。
请确保将 `json` 替换为你实际的 JSON 字符串,并根据需要调整处理后的对象的逻辑。
阅读全文