反序列化复杂的JSON结果

时间:2017-02-16 20:08:08

标签: c# json

我正在自动化API。我想反序列化我得到的JSON响应,但我无法弄清楚如何做到这一点。这是我得到的JSON

{
  "class": [
    "Paged-Collection",
    "Products"
  ],

  "properties": {
    "defaultPageSize": 10,
    "skip": null,
    "top": 10,
    "totalRows": 2
  },

  "entities": [
    {
      "class": [
        "Product"
      ],
      "rel": [],
      "properties": {
        "Supplier": "Supplier1",
        "Currency": "USD",
        "ProductCode": "SomeCode1",
        "price": 2
      }
    },
    {
      "class": [
        "Product"
      ],
      "rel": [],
      "properties": {
        "Supplier": "Supplier2",
        "Currency": "USD",
        "ProductCode": "SomeCode2",
        "price": 73
      }
    },
  ],
  "links": [
    {
      "rel": [
        "self"
      ],
      "href": "http://localhost...",
      "method": "GET",
      "title": null,
      "type": null
    }
  ]
}

从我在其他例子中看到的反序列化归结为确定确切的数据结构(不确定,不管它是什么)然而我做了几次尝试(不只是急于回答SO),所以这是我的最后一次尝试:

我看到的结构:

public class Data
{
    List<string> @class { get; set; }
    Dictionary<string, MetaData> properties { get; set; }
    List<entities> entities { get; set; }
    List<LinkFeed> links { get; set; }
}
public class MetaData
{
    public int defaultPageSize { get; set; }
    public string skip { get; set; }
    public int top { get; set; }
    public int totalRows { get; set; }
}
public class entities
{
    public List<string> @class { get; set; }
    public List<string> rel { get; set; }
    public properties property { get; set; }
}

public class properties
{
    public string Supplier { get; set; }
    public string Currency { get; set; }
    public string ProductCode { get; set; }
    public string price { get; set; }
    public double Price { get; set; }
}

public class LinkFeed
{
    public List<string> rel { get; set; }
    public string href { get; set; }
    public string method { get; set; }
    public string title { get; set; }
    public string type { get; set; }
}

使用Newtonsoft.Json进行实际反序列化:

var result = JsonConvert.DeserializeObject<Data>(data);

一切都是Null ..

实际上,我真正感兴趣的数据是"entities"下的数据,但我不知道是否只提取实际上更容易或更困难的数据。

1 个答案:

答案 0 :(得分:2)

这是你的结构:

 Imports System.Net
Imports System.ComponentModel
Imports Awesomium.Core



Public Class Form1
    Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        WebCore.Shutdown()
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If (My.Settings.serial.Length < 3) Then
            Dim serial As String = New WebClient().DownloadString("https://***********.php")

            My.Settings.serial = serial

        End If
        WebControl1.Source = New Uri("https://**************.php?sth=abc&serial=" & My.Settings.serial, UriKind.Absolute)
    End Sub
End Class

所以你会这样做:

public class Rootobject {
   public string[] _class { get; set; }
   public Properties properties { get; set; }
   public Entity[] entities { get; set; }
   public Link[] links { get; set; }
}

public class Properties {
   public int defaultPageSize { get; set; }
   public object skip { get; set; }
   public int top { get; set; }
   public int totalRows { get; set; }
}

public class Entity {
   public string[] _class { get; set; }
   public object[] rel { get; set; }
   public Properties1 properties { get; set; }
}

public class Properties1 {
   public string Supplier { get; set; }
   public string Currency { get; set; }
   public string ProductCode { get; set; }
   public int price { get; set; }
}

public class Link {
   public string[] rel { get; set; }
   public string href { get; set; }
   public string method { get; set; }
   public object title { get; set; }
   public object type { get; set; }
}

我会将类var result = JsonConvert.DeserializeObject<Rootobject>(data); 重命名为另一个名称,因为它会与程序集的Properties类冲突。这会给你带来麻烦,只是为了安全地称之为别的东西。

另外如果您不喜欢上述类中的某些属性名称,我的意思是我不喜欢它,因为它不遵循.NET命名约定,那么您可以这样做:

Properties

显然,将其称为public class Rootobject { [JsonProperty( "class" )] public string[] MyClass { get; set; } // ... other properties } 只是一个示例,因此请为其命名以反映业务领域。