在C#中从更大的json创建缩减的json [英] Creating reduced json from a bigger json in c#

查看:89
本文介绍了在C#中从更大的json创建缩减的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a json that is coming from the server similar to this (with multiple nested json objects).

{
"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.

The service component will send only the relevant data in a reduced json format to the UX.
I want to extract employee.fullname.firstname and 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等

I flattened the Json but it gives the tags as employee.0.fullname.firstname and employee.0.project.0.projectname etc

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

What is the best way to extract with/without flattening?

推荐答案

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

You can create small small interfaces, first convert json in to class object and then typecast into the specific Interface, and then again serialize it using newtonSoft, it will give small JSON

这篇关于在C#中从更大的json创建缩减的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆