3

我想反序列化类似于以下内容的 JSON 数据(使用 Newtonsoft),并转换为 C# 中的强类型对象/列表,但无法弄清楚如何定义类以便将索引引用转换为引用的对象。

{
  "Countries": [
    {
      "Name": "USA",

    },
    {
      "Name": "UK",
    },
    {
      "Name": "JAPAN",
    },
  ],
  "Authors": [
    {
      "DisplayName": "John Doe",
      "RealName": "Not John Doe"
    },
    {
      "DisplayName": "Jane Doe",
      "RealName": "Not Jane Doe"
    },
  ],
  "Books": [
    {
      "Author": 0,
      "Title": "A good read",
      "PublishedCountries": "0,1",

    },
    {
      "Author": 0,
      "Title": "Another good read",
      "PublishedCountries": "0,1",
    },
    {
      "Author": 1,
      "Title": "This one is even better",
      "PublishedCountries": "0,1,2",
    },
  ],
}

理想情况下,我想使用类似于以下的类:

public class Country
{
  public string Name { get; set;}
}

public class AuthorDetails
{
  public string DisplayName { get; set; }
  public string RealName { get; set; }
}

public class Book
{
  public AuthorDetails Author { get; set; }
  public string Title { get; set; }
  public IEnumerable<Country> PublishedCountries { get; set; }
}

public class ListOfBooks
{
  public IEnumerable<Book> Books { get; set; }
}

并像这样反序列化:

var listOfBooks = JsonConvert.DeserializeObject<ListOfBooks>(jsonAsString);

我被困在如何告诉 Json.Net JObject 书中的 Author 属性是一个索引,而不是一个整数。PublishedCountries 也是如此(以逗号分隔的索引列表)

4

2 回答 2

1

除了帮助反序列化过程之外,我看不到其他方法。

var dynObj = (JObject)JsonConvert.DeserializeObject(json);

var authors = dynObj["Authors"]
                .Select(j => new AuthorDetails { 
                            RealName = (string)j["RealName"], 
                            DisplayName = (string)j["DisplayName"] 
                })
                .ToList();

var countries = dynObj["Countries"]
                .Select(j => new Country { Name = (string)j["Name"]})
                .ToList();

var books = dynObj["Books"].Select(x => new Book
                {
                    Author = authors[(int)x["Author"]],
                    Title = (string)x["Title"],
                    PublishedCountries = x["PublishedCountries"].ToString().Split(',')
                                            .Select(i =>countries[int.Parse(i)])
                                            .ToList()
                })
                .ToList();

public class Country
{
    public string Name { get; set; }
}

public class AuthorDetails
{
    public string DisplayName { get; set; }
    public string RealName { get; set; }
}

public class Book
{
    public AuthorDetails Author { get; set; }
    public string Title { get; set; }
    public List<Country> PublishedCountries { get; set; }
}
于 2012-10-09T09:39:30.863 回答
0

我相信您想要做的是“保留对象引用”。

您可以使用属性标记您的 C# 对象以描述何时使用引用,而不是重新复制对象的内容:

[JsonObject(IsReference = true)](用于类型声明)

但是,您生成的 Javascript 看起来与您演示的不太一样,请查看此处以了解如何操作。

http://james.newtonking.com/projects/json/help/?topic=html/PreserveObjectReferences.htm

相反,您的 JSON 看起来更像这样:

{
  "Countries": [
    {
      "Name": "USA",

    },
    {
      "Name": "UK",
    },
    {
      "Name": "JAPAN",
    },
  ],
  "Authors": [
    {
      "DisplayName": "John Doe",
      "RealName": "Not John Doe"
    },
    {
      "DisplayName": "Jane Doe",
      "RealName": "Not Jane Doe"
    },
  ],
  "Books": [
    {
      "$id": 1,
      "Author": 0,
      "Title": "A good read",
      "PublishedCountries": "0,1",

    },
    {
      "$ref": 1
    },
    {
      "Author": 1,
      "Title": "This one is even better",
      "PublishedCountries": "0,1,2",
    },
  ],
}
于 2012-10-09T03:39:17.350 回答