Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将一个JSON结构转换为另一个,并在其中添加一个额外字段_C#_Json - Fatal编程技术网

C# 如何将一个JSON结构转换为另一个,并在其中添加一个额外字段

C# 如何将一个JSON结构转换为另一个,并在其中添加一个额外字段,c#,json,C#,Json,我有来自API的json数据。json的结构如下 { "data": [ { "nameid": "s_10", "size": "6.46", "name": "abc", "children": [ { "nameid": "i_101010", "size": "8.84", "name": "bcd

我有来自API的json数据。json的结构如下

{
"data": [
    {
        "nameid": "s_10",
        "size": "6.46",
        "name": "abc",
        "children": [
            {
                "nameid": "i_101010",
                "size": "8.84",
                "name": "bcd",
                "children": [
                    {
                        "nameid": "si_10101010",
                        "size": "4.00",
                        "name": "efg",
                        "children": [
                            {
                                "nameid": "c_3273",
                                "size": 4,
                                "name": "ttt",
                            }
                        ]
                    },
                    {
                        "nameid": "si_10101020",
                        "size": "13.67",
                        "name": "sss",
                        "children": [
                            {
                                "nameid": "c_4450",
                                "size": 1,
                                "name": "rrr",
                            },
                            {
                                "nameid": "c_551",

                                "size": 17,
                                "name": "ddd",

                            },
我需要把它转换成下面的结构

  {
"id": 1,
"parentId": "NULL",
"name": "Root",
"size": 5,
"children": [
{
"id": 10,
"parentId": "1",
"name": "En",
"size": 1,
"children": [
 {
  "id": 1010,
  "parentId": "10",
  "name": "Eee",
  "size": 1,
  "children": [
   {
    "id": 101010,
    "parentId": "1010",
    "name": "Enh",
    "size": 5,
    "children": [
     {
      "id": 10101010,
      "parentId": "101010",
      "name": "Ooo",
      "size": 5
     },
简而言之,我需要将该字段的父id添加到第一个JSON中


注意:这些是部分数据,因此json可能不是有效数据。

如果源对象和目标对象是类型化类(例如公共类家庭成员

Automapper是实现这一目标的最佳方式

假设您的源“数据”是class家族。您可以将家族映射到家族成员

//specify source, destination
Mapper.CreateMap<Families, FamilyMembers>(); 

//get source data
Families families = GetFamiliesFromAPI(); 

//map each properties of  families to familymembers
FamilyMembers familymembers = Mapper.Map<Families, FamilyMembers>(families); 

ShowFamilyMembersInDataGrid(familymembers);

因为您有嵌套的JSON对象,所以需要学习如何处理诸如“children”之类的对象“如果通过LINQ,源数据和映射中的成员相应地

Json不是XML。没有任何东西能像XSLT那样允许您将一个Json版本转换为另一个Json版本。一个选项是反序列化源并映射到目标DTO。另一种方法是遍历XML并使用XSLT进行转换。Json.NET支持从/到XML的序列化。如图所示
Families families = GetFamiliesFromAPI(); 
families.Select(f => f, new FamilyMembers() {name = f.Name, someProperty = f.someProperty }