将两个稍微不同的JSON字符串(相同的结构,不同的名称)反序列化为同一个类

Jon*_*nas 5 c# json.net deserialization

我的问题很简单:我需要将两个大的JSON字符串反序列化为一个类,但字符串略有不同.这是第一个:

 {  
  "persons": [  
    {  
      "age":30,
      "name":"david",
      "hobbies": [  
        {  
          "name":"tennis",
          "hours":5
        },
       {  
         "name":"football",
         "hours":10
       }
     ]
   },
   {  
     "name":"adam",
     "age":23,
     "hobbies":[]
   }
 ]   
}  
Run Code Online (Sandbox Code Playgroud)

另一个:

{  
  "person": [  
    {  
      "age":25,
      "name":"dave",
      "hobbies":[  
        {  
          "name":"Basketball",
          "hours":5
        },
        {  
          "name":"football",
          "hours":10
        }
      ]
    },
    {  
      "name":"Steve",
      "age":28,
      "hobbies": []
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

你可以看到一次是"人",另一次是"人".这有什么简单的解决方案吗?我在考虑在班上创建两个列表

List<Person> person;
List<Person> persons;
Run Code Online (Sandbox Code Playgroud)

然后在反序列化后以某种方式手动组合.但必须有一个更简单的方法.

顺便说一句,这不是我需要反序列化的确切代码.我只是想让主要想法尽可能简单.

Bri*_*ers 3

一个简单的解决方案是在类中使用单个列表,然后使用引用同一列表的其他名称添加备用设置器。你甚至可以将其设为私有,只要用[JsonProperty]属性修饰即可。这样,类的公共接口看起来会很正常,但它仍然可以使用两个 JSON 属性名称。

public class RootObject
{
    [JsonProperty("persons")]
    public List<Person> People { get; set; }

    // This is visible to Json.Net and references the real list
    [JsonProperty("person")]
    private List<Person> Person
    {
        set { People = value; }
    }
}

public class Person
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("age")]
    public int Age { get; set; }

    [JsonProperty("hobbies")]
    public List<Hobby> Hobbies { get; set; }
}

public class Hobby
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("hours")]
    public int Hours { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

小提琴: https: //dotnetfiddle.net/9Dw48J

编辑

解决您的评论:PopulateObject可用于用 JSON 数据盲目地扩充现有对象,但它不会执行您正在寻找的那种合并。您似乎正在寻找一种通过姓名(也许还有年龄?)来匹配人们的方法,然后结合这些人的爱好。为此,您需要编写自己的逻辑。我建议将每个列表反序列化为类的单独实例RootObject,然后在后处理步骤中合并数据。您可以在类上创建一个方法RootObject,该方法将接受另一个方法RootObject进行合并。也许是这样的:

public class RootObject
{
    ...

    public void MergeWith(RootObject other)
    {
        if (other.People == null) return;
        if (People == null) People = new List<Person>();
        foreach (Person person in other.People)
        {
            // You may need to make changes here--
            // How do you determine whether two people are the same?
            Person existingPerson = People.FirstOrDefault(p => p.Name == person.Name && 
                                                               p.Age == person.Age);
            if (existingPerson != null)
            {
                existingPerson.MergeWith(person);
            }
            else
            {
                People.Add(person);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

还有在Person课堂上...

public class Person
{
    ...

    public void MergeWith(Person other)
    {
        if (other.Hobbies == null) return;
        if (Hobbies == null) Hobbies = new List<Hobby>();
        foreach (Hobby hobby in other.Hobbies)
        {
            Hobby existingHobby = Hobbies.FirstOrDefault(h => h.Name == hobby.Name);
            if (existingHobby != null)
            {
                // You may need to make changes here--
                // What do you do if two hobbies have the same name but different hours?
                existingHobby.Hours += hobby.Hours;
            }
            else
            {
                Hobbies.Add(hobby);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要反序列化和合并,您可以执行以下操作:

var firstObj = JsonConvert.DeserializeObject<RootObject>(firstJson);
var secondObj = JsonConvert.DeserializeObject<RootObject>(secondJson);
firstObj.MergeWith(secondObj);
Run Code Online (Sandbox Code Playgroud)

小提琴: https: //dotnetfiddle.net/8Fiwsd