如何在Json中获取给定后代的所有数组值 [英] how to get all the array values for a given descendant in a Json
问题描述
{
"hasLoadMore": true,
"groups": [
{
"order": 0,
"title": "string",
"total": 0,
"dateFormat": "string",
"messages": [
{
"commsId": 0,
"commsDirectionCode": "string",
"commsReasonCRSNN": 0,
"commsDeliveryChannelCDCHN": "string",
"commsDeliveryStateCode": "string",
"commsDeliveryDttm": "2017-02-14T02:20:52.836Z",
"commsSuccessFlag": true,
"commsCode": "string",
"commsName": "string",
"commsRankingNumb": 0,
"customerGrainCUSGN": 0,
"productItemCode": "string",
"sourceApplBAPPN": 0,
"sourceCommsRefn": "string",
"parentCommsId": 0,
"campaignDttm": "2017-02-14T02:20:52.836Z",
"campaignCode": "string",
"campaignName": "string",
"contentValidStartDttm": "2017-02-14T02:20:52.836Z",
"contentValidEndDttm": "2017-02-14T02:20:52.836Z",
"trackingCode": "string",
"headlineDisplayText": "string",
"contentDisplayText": "string",
"topic": "string",
"topicIconClass": "string",
"contentTemplateId": 0,
"contentTemplateVersNumb": 0,
"archiveDate": "2017-02-14T02:20:52.836Z",
"commsParameters": [
{
"commsId": 0,
"paramSeqNumb": 0,
"paramText": "string",
"paramValue": "string"
}
],
"contentTemplateParameters": [
{
"commsId": 0,
"paramSeqNumb": 0,
"paramText": "string",
"paramValue": "string"
}
],
"callToAction": [
{
"displayText": "string",
"uri": "string"
}
],
"isFlagged": true,
"isSeen": true,
"isRead": true,
"isActioned": true
}
]
}
]
}
我的json在上面,现在我在上面的json中有多个'callToActions'
数组,想要收集所有它们并与一个表进行比较.
my json is above , now i have multiple 'callToActions'
array in the above json and want to collect all of them and compare with a table.
我在做什么如下:
{
string content = Context.Response.Content.ReadAsStringAsync().Result;
var resultObjects = AllChildren(JObject.Parse(content))
.First(c => c.Type == JTokenType.Array && c.Path.Contains(decendant))
.Children<JObject>();
JArray responseList = new JArray();
foreach (JObject result in resultObjects)
{
responseList.Add(result);
}
CompareObjectToTable(responseList, table);
}
private static IEnumerable<JToken> AllChildren(JToken decendant)
{
foreach (var c in decendant.Children())
{
yield return c;
foreach (var cc in AllChildren(c))
{
yield return cc;
}
}
}
但是它只会给我遇到的"First"
数组,但我想收集具有相同后代的所有数组(多个).我想要的后代是callToActions
But it will only give me the "First"
array encountered but i want to gather all the array(multiple) with same descendant.Here descendant i want is callToActions
推荐答案
您可以使用内置方法 JToken.SelectTokens()
为您做到这一点.
Rather than hand-coding your own method to recursively descend the JToken
hierarchy, you can use the built-in methods JContainer.DescendantsAndSelf()
or JToken.SelectTokens()
to do it for you.
使用第一种方法,您可以这样做:
Using the first approach, you would do:
var root = (JContainer)JToken.Parse(content);
var descendant = "callToAction";
var query = root
// Recursively descend the JSON hierarchy
.DescendantsAndSelf()
// Select all properties named descendant
.OfType<JProperty>()
.Where(p => p.Name == descendant)
// Select their value
.Select(p => p.Value)
// And filter for those that are arrays.
.OfType<JArray>();
并使用第二种方法:
var root = JToken.Parse(content);
var descendant = "callToAction";
var query = root
// Recursively descend the JSON hierarchy using the JSONpath recursive descent operator "..", and select the values of all properties named descendant
.SelectTokens(string.Format("..{0}", descendant))
// And filter for those that are arrays.
.OfType<JArray>();
请注意,我正在使用 JSONPath 递归下降运算符".."
来递归层次结构. Json.NET的SelectTokens()
方法支持JSONPath查询语法类似于XPath.
Note that I am using the JSONPath recursive descent operator ".."
to descend the hierarchy. Json.NET's SelectTokens()
method supports JSONPath query syntax which is somewhat similar to XPath.
现在,不清楚您的问题是responseList
应该是结果数组还是平面结果数组.要创建一个数组数组,只需执行以下操作:
Now, it's not clear from your question whether responseList
should be an array of arrays of results, or a flat array of results. To create an array of arrays, just do:
var responseList = new JArray(query);
要创建展平的数组,请执行以下操作:
To create a flattened array, do:
var responseList = new JArray(query.SelectMany(a => a));
示例小提琴.
这篇关于如何在Json中获取给定后代的所有数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!