如何检查 JSON 响应中的嵌套对象列表是否为空?
How to check if a nested list of objects inside a JSON response is empty or not?
我有一个 JSON 我从
得到的
var responseMessage1 = await request1.Content.ReadAsStringAsync();
看起来像这样:-
{
"operations": [
{
"creationTime": "2022-06-02T10:28:28.765+03:00",
"deviceId": "43432103",
"deviceName": "P25-SC-0228",
"status": "PENDING",
"com_cumulocity_model": {
"op": "s",
"param": "waterStartDateTime",
"value": "1/2/2018, 7:30:00 AM"
},
"description": "Generate Plan"
},
{
"creationTime": "2022-06-02T10:28:36.276+03:00",
"deviceId": "43432103",
"deviceName": "P25-SC-0228",
"status": "PENDING",
"com_cumulocity_model": {
"Mode": 0,
"StopStationPayload": "[{\"ControllerAddress\":11,\"StationAddress\":26}]"
},
"description": "Stop Station"
}
],
"statistics": {
"currentPage": 1,
"pageSize": 5
}
}
我想要实现的是检查 json 中的操作列表是否为空的条件,其中 json 可能如下所示:-
{
"operations": [],
"statistics": {
"currentPage": 1,
"pageSize": 1
}
}
我想执行类似
的操作
if(responseMessage1["operations"] != null){
//do something
}
您可以使用 JToken
的 HasValues
属性 进行分支
if (json["operations"].HasValues)
我有一个 JSON 我从
得到的var responseMessage1 = await request1.Content.ReadAsStringAsync();
看起来像这样:-
{
"operations": [
{
"creationTime": "2022-06-02T10:28:28.765+03:00",
"deviceId": "43432103",
"deviceName": "P25-SC-0228",
"status": "PENDING",
"com_cumulocity_model": {
"op": "s",
"param": "waterStartDateTime",
"value": "1/2/2018, 7:30:00 AM"
},
"description": "Generate Plan"
},
{
"creationTime": "2022-06-02T10:28:36.276+03:00",
"deviceId": "43432103",
"deviceName": "P25-SC-0228",
"status": "PENDING",
"com_cumulocity_model": {
"Mode": 0,
"StopStationPayload": "[{\"ControllerAddress\":11,\"StationAddress\":26}]"
},
"description": "Stop Station"
}
],
"statistics": {
"currentPage": 1,
"pageSize": 5
}
}
我想要实现的是检查 json 中的操作列表是否为空的条件,其中 json 可能如下所示:-
{
"operations": [],
"statistics": {
"currentPage": 1,
"pageSize": 1
}
}
我想执行类似
的操作if(responseMessage1["operations"] != null){
//do something
}
您可以使用 JToken
的 HasValues
属性 进行分支
if (json["operations"].HasValues)