如何在另一个JSONPATH表达式内使用JSONPATH表达式作为过滤器



我想通过使用嵌入过滤器中的另一个表达式过滤JSON Pather表达式来从JSON字符串中选择对象。换句话说,我想过滤JSON数据中其他位置的值。

例如:

在以下JSON数据中, $.Item.State.stepId 中有一个值(当前" QG2.0")。我需要具有基于此值选择值的jsonpath表达式,例如:

$..Step[?(@.stepId==$Item.State.stepId)].actionDate

,但这不会返回任何结果。如果我直接使用字符串(" QG2.0"),则可以:

$..Step[?(@.stepId=='QG2.0')].actionDate

它将返回所需的数据。

怎么了,还是不可能?我的json在下面:

{
    "Item": {
            "Common": {
                    "folio": "PSH-000016020",
                    "setName": "123-XZ200-1",
                    "wfId": "Kat1_002",
                    "wfIssue": "002",
                    "wfIdIssue": "Kat1_002.002"
            },
            "State": {
                    "status": "IN WORK",
                    "stepId": "QG2.0",
                    "stepDescription": "Validation"
            },
            "Participants": {
                    "Participant": [
                            {
                                    "role": "PR",
                                    "roleDescription": "Product Responsible",
                                    "loginName": "marc102",
                                    "email": "mark@abc.de"
                            }, {
                                    "role": "CR",
                                    "roleDescription": "Chapter Responsible",
                                    "loginName": "uli26819",
                                    "email": "uli@abc.de"
                            }
                    ]
            },
            "Steps": {
                    "Step": [
                            {
                                    "stepId": "QG1.0",
                                    "stepTitle": "Preparation",
                                    "actionDate": "2016-06-28T10:28:09",
                                    "actionDueDate": "",
                                    "actionBy_Name": "Marc",
                                    "actionBy_Account": "marc102",
                                    "action": "complete",
                                    "Comment": ""
                            }, {
                                    "stepId": "QG2.0",
                                    "stepTitle": "Check Requirements",
                                    "actionDate": "2016-08-08T14:17:04",
                                    "actionDueDate": "",
                                    "actionBy_Name": "Uli",
                                    "actionBy_Account": "uli26819",
                                    "action": "complete",
                                    "Comment": ""
                            }
                    ]
            }
    }
}

我不认为JSON.NET的JSONPATH实现支持此概念。
但是,如果将查询分为两个步骤,您仍然可以获取所需的信息:

JObject obj = JObject.Parse(json);
JToken stepId = obj.SelectToken("Item.State.stepId");
JToken actionDate = obj.SelectToken(string.Format("$..Step[?(@.stepId=='{0}')].actionDate", stepId));
Console.WriteLine(actionDate.ToString());

小提琴:https://dotnetfiddle.net/kunytf

相关内容

  • 没有找到相关文章

最新更新