在C#

时间:2019-06-26 01:36:39

标签: c# json linq json.net

我有一个来自服务器的json与此类似(带有多个嵌套的json对象)。

{
"employee": [{
        "fullname": {
            "firstname": "abcd",
            "lastname": "defg"
        },
        "project": [{
                "projectname":"abcd_1",
                "datejoined": "2019-06-18T01:29:38.6013262+00:00",
                "projectmanager": "abcdM1",
            }, {
                "projectname":"abcd_2",
                "datejoined": "2018-06-18T01:29:38.6013262+00:00",
                "projectmanager": "abcdM2",
            }, {
                "projectname":"abcd_3",
                "datejoined": "2017-06-18T01:29:38.6013262+00:00",
                "projectmanager": "abcdM3",
            }
        ]
    },{
        "fullname": {
            "firstname": "abcd",
            "lastname": "defg"
        },
        "project": [{
                "projectname":"abcd_1",
                "datejoined": "2019-06-18T01:29:38.6013262+00:00",
                "projectmanager": "abcdM1",
            }, {
                "projectname":"abcd_2",
                "datejoined": "2018-06-18T01:29:38.6013262+00:00",
                "projectmanager": "abcdM2",
            }, {
                "projectname":"abcd_3",
                "datejoined": "2017-06-18T01:29:38.6013262+00:00",
                "projectmanager": "abcdM3",
            }
        ]
      }
    ]

}

服务组件将仅以简化的json格式将相关数据发送到UX。
我要提取employee.fullname.firstname和employee.project.projectname。

输出应为

{
"employee": [{
        "fullname": {
            "firstname": "abcd",
        },
        "project": [{
                "projectname":"abcd_1",
            }, {
                "projectname":"abcd_2",
            }, {
                "projectname":"abcd_3",
            }
        ]
    },{
        "fullname": {
            "firstname": "abcd",

        },
        "project": [{
                "projectname":"abcd_1",

            }, {
                "projectname":"abcd_2",

            }, {
                "projectname":"abcd_3",

            }
        ]
      }
    ]

}

我拼合了Json,但它给出的标签为employee.0.fullname.firstname和employee.0.project.0.projectname等

有/没有展平的最佳提取方法是什么?

2 个答案:

答案 0 :(得分:0)

这是一个仅使用具有所需属性的poco的示例,您可以在此对象中反序列化此对象,然后序列化回json以获取所需内容。

void Main()
{
    var myJson = @"
    {
        ""employee"": [{
            ""fullname"": {
                ""firstname"": ""abcd"",
                ""lastname"": ""defg""
            },
            ""project"": [{
                    ""projectname"":""abcd_1"",
                    ""datejoined"": ""2019-06-18T01:29:38.6013262+00:00"",
                    ""projectmanager"": ""abcdM1"",
                }, {
                    ""projectname"":""abcd_2"",
                    ""datejoined"": ""2018-06-18T01:29:38.6013262+00:00"",
                    ""projectmanager"": ""abcdM2"",
                }, {
                    ""projectname"":""abcd_3"",
                    ""datejoined"": ""2017-06-18T01:29:38.6013262+00:00"",
                    ""projectmanager"": ""abcdM3"",
                }
            ]
        },{
            ""fullname"": {
                ""firstname"": ""abcd"",
                ""lastname"": ""defg""      
            },
            ""project"": [{
                    ""projectname"":""abcd_1"",
                    ""datejoined"": ""2019-06-18T01:29:38.6013262+00:00"",
                    ""projectmanager"": ""abcdM1"",
                }, {
                    ""projectname"":""abcd_2"",
                    ""datejoined"": ""2018-06-18T01:29:38.6013262+00:00"",
                    ""projectmanager"": ""abcdM2"",
                }, {
                    ""projectname"":""abcd_3"",
                    ""datejoined"": ""2017-06-18T01:29:38.6013262+00:00"",
                    ""projectmanager"": ""abcdM3"",
                }
            ]
          }
        ]
    }";


    var myObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Employee>(myJson);
    var myTrimmedJson = Newtonsoft.Json.JsonConvert.SerializeObject(myObject);
    Console.WriteLine(myTrimmedJson);
}

public class Fullname
{
    public String firstname { get; set; }
    //public String lastname { get; set; }
}

public class Project
{
    public String projectname { get; set; }
    //public String datejoined { get; set; }
    //public String projectmanager { get; set; }
}

public class Person
{
    public Fullname fullname { get; set; }
    public List<Project> project { get; set; }
}

public class Employee
{
    public List<Person> employee { get; set; }
}

答案 1 :(得分:0)

您可以创建小型的小型接口,首先将json转换为类对象,然后类型转换为特定的Interface,然后再次使用newtonSoft对其进行序列化,它将提供小型JSON