简体   繁体   中英

Convert IEnumerable<JToken> to IEnumerable<String>

I have this sample Json:

IEnumerable<JToken> a = JArray.Parse("[{'key1':'value1', 'key2':'value2'}]");

I want to convert it to IEnumerable<String> by concatenating all the values. I tried following LINQ query, it performs concatenation correctly but it returns back IEnumerable<JToken>

var t = a.Select(x => x.Values().Aggregate((i, j) => $"{i}|{j}")).ToList();

How do I convert to IEnumerable<String> ?

I think if you select the token's Value<string> method (which converts it to a string ), it should give you what you're looking for:

List<string> result = a
    .Select(token => token.Values().Aggregate((i, j) => $"{i}|{j}").Value<string>())
    .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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