我在C#中有一个json对象(表示为Newtonsoft.Json.Linq.JObject对象),我需要将其展平为字典.让我举例说明我的意思:
{
"name": "test",
"father": {
"name": "test2"
"age": 13,
"dog": {
"color": "brown"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这应该产生一个包含以下键值对的字典:
["name"] == "test",
["father.name"] == "test2",
["father.age"] == 13,
["father.dog.color"] == "brown"
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我想一般扁平一些json所以我可以转换为数据表并使用c#绑定到数据网格
什么是最好的办法,记住我不知道我要下多少级别?
例如
{
"appointmentid": 4,
"policyid": 1,
"guid": "00000000-0000-0000-0000-000000000000",
"number": "1234567890",
"ampm": "false",
"date": "2015-09-08T00:00:00",
"vehicle": {
"id": 1,
"guid": "00000000-0000-0000-0000-000000000000",
"make": null,
"model": null
},
"installer": {
"installerid": "1",
"name": "Installer 1",
"contact": "qwerty",
"qascore": "0",
"address1": "qwerty",
"address2": "qwerty",
"address3": null,
"address4": null,
"city": "qwertyu",
"county": "qwertyu",
"postcode": "asdfghj",
"country": "GB",
"email": "asdfghj",
"web": "asdfghjk",
"archived": false
},
"installations": [
{
"installationid": 6,
"installationstatus": {
"installationstatusid": 4,
"installationstatus": "FAIL"
},
"isactive": true
},
{
"installationid": 7,
"installationstatus": … 我有一个IEnumerable集合,它是分层的,因为一个元素包含几个元素.因此,如果我进行计数,我可能得到7-8作为返回int,当真的可能有500个项目(因为它们是嵌套的).
如何将此集合拼成一个包含所有元素且没有嵌套的集合?
谢谢
我有一个IEnumerable<object>可能包含或不包含一些嵌套集合.例如,我的起点可能如下所示:
[ "foo", 2, [1, 2, 3, 4], "bar" ]
Run Code Online (Sandbox Code Playgroud)
我想把它压平为:
[ "foo", 2, 1, 2, 3, 4, "bar" ]
Run Code Online (Sandbox Code Playgroud)
我认为SelectMany应该在这里工作但是找不到合适的组合.我可以蛮力,但我认为应该有一个更优雅的解决方案.