简体   繁体   English

在C#中将json对象列表反序列化为POCO对象

[英]Deserializing list of json objects to POCO objects in C#

I am making a call to a servie that returns a list of jso serialized objects such as this: 我正在调用一个servie,它返回一个jso序列化对象列表,如下所示:

{"employees":[{"employee":{"id":"1","date_created":"2011-06-16T15:03:27Z","extended":[{"address":{"street1":"12345 first st.","city":"Denver","state":"CO"}}]}},{"employee":{"id":"2"... { “雇员”:[{ “雇员”:{ “ID”: “1”, “DATE_CREATED”: “2011-06-16T15:03:27Z”, “扩展”:[{ “地址”:{ “street1” :“12345 first st。”,“city”:“Denver”,“state”:“CO”}}]}},{“employee”:{“id”:“2”......

So, you can see I first-off have a list of employee objects called employees. 所以,你可以看到我首先得到一个名为employees的员工对象列表。 On top of that, each employee object contains another object called extended for extended info (in this case address info). 最重要的是,每个雇员对象包含另一个名为extended的对象,用于扩展信息(在本例中为地址信息)。 What I would like to acheive is passing in the entire list as a string to a deserializer and getting back a List with the Employee object looking like this: 我想要实现的是将整个列表作为字符串传递给反序列化器并使用Employee对象返回List,如下所示:

[Serializable]
public class Employee    {
    public string Id { get; set; }
    public string DateCreated { get; set; }
    public ExtendedProperties Address { get; set; }
}

[Serializable]
public class ExtendedProperties
{
    public string Street1 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

I have found similar examples using NEwtonSoft but they are not quite the same in terms of the composite object. 我找到了使用NEwtonSoft的类似例子,但就复合对象而言,它们并不完全相同。 If needed I can drop the extended properties. 如果需要,我可以删除扩展属性。 But that would be far from ideal. 但这远非理想。

Any help would be greatly appreciated. 任何帮助将不胜感激。

TIA! TIA!

Well, there are a few things here: 嗯,这里有一些事情:

  • You've got an outer "wrapper" level or two, mapping the employees property to the actual collection of properties. 您有一个或两个外部“包装器”级别,将employees属性映射到实际的属性集合。 You can either deal with that with a separate class, or read the whole thing using LINQ to JSON and then dig in one layer before deserializing the collection. 您可以使用单独的类处理它,也可以使用LINQ to JSON读取整个内容,然后在反序列化集合之前挖掘一个层。
  • It looks like you've actually got a collection of extended properties per employee, with one property of extended properties being the address. 看起来你实际上每个员工都有一个扩展属性的集合 ,扩展属性的一个属性就是地址。
  • I'm not sure how to persuade the JSON library to convert date_created to DateCreated , although I dare say it could. 我不知道如何说服JSON库将date_created转换为DateCreated ,尽管我敢说它可以。

I've hacked up something to read this - but it's a bit ugly: 我已经讨好了一些东西来读这个 - 但它有点难看:

using System;
using System.Collections.Generic;
using System.IO;

using Newtonsoft.Json;

public class EmployeeCollection {
    public List<EmployeeWrapper> Employees { get; set; }
}

public class EmployeeWrapper {
    public Employee Employee { get; set; }
}

public class Employee    {
    public string Id { get; set; }
    public string Date_Created { get; set; }
    public List<ExtendedProperty> Extended  { get; set; }
}

public class ExtendedProperty {
    public Address Address { get; set; }
}

public class Address
{
    public string Street1 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

class Test
{ 
    static void Main() 
    {
        string json = @"{""employees"":
            [{""employee"":
                {""id"":""1"",
                 ""date_created"":""2011-06-16T15:03:27Z"",
                 ""extended"":[
                    {""address"":
                    {""street1"":""12345 first st."",
                     ""city"":""Denver"",
                     ""state"":""CO""}}]
              }}]}";


        var employees =
             JsonConvert.DeserializeObject<EmployeeCollection>(json);
        foreach (var employeeWrapper in employees.Employees)
        {
            Employee employee = employeeWrapper.Employee;
            Console.WriteLine("ID: {0}", employee.Id);
            Console.WriteLine("Date created: {0}", employee.Date_Created);
            foreach (var prop in employee.Extended)
            {
                Console.WriteLine("Extended property:");
                Address addr = prop.Address;
                Console.WriteLine("{0} / {1} / {2}", addr.Street1,
                                  addr.City, addr.State);
            }
        }
    }     
}

If you want to keep your original class structure, I suggest you use LINQ to JSON to do a more manual conversion. 如果你想保留原始的类结构,我建议你使用LINQ to JSON进行更多的手动转换。 It's not terribly hard, when you get used to the JSON library - particularly if you're happy with LINQ to Objects. 当你习惯了JSON库时,这并不是特别困难 - 特别是如果你对LINQ to Objects感到满意的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM