簡體   English   中英

在 c# 處反序列化復雜 JSON

[英]Deserialize complex JSON at c#

我從 API 得到這個 object

{
        "addedAt": 1573172075745,
        "vid": 12590024,
        "canonical-vid": 12590024,
        "merged-vids": [],
        "portal-id": 62515,
        "is-contact": true,
        "profile-token": "AO_T-mOW3t21rXoDQIhBpPulGUoTyxQKLbBxaHrS2P3MsXjF4qFJ9BIvIgkVDpeha5P3GHujF8FOP-0XFRndATAU_YogQKRBTDddOFx8s_DITNLUrnfU07QCwW61HUPygEAyDeNG6N8d",
        "profile-url": "https://app.hubspot.com/contacts/62515/contact/12590024",
        "properties": {
            "firstname": {
                "value": "Matt"
            },
            "lastmodifieddate": {
                "value": "1573172075745"
            },
            "company": {
                "value": "HubSpot"
            },
            "lastname": {
                "value": "Schnitt"
            }
        }
    }

並嘗試填寫此 model

class UserProperties
{
    public string firstname { get; set; }
    public DateTime lastmodifieddate { get; set; }
    public string company { get; set; }
    public string lastname { get; set; }
}
class UserForDeserialize
{
    public int vid { get; set; }
    public List<UserProperties> properties { get; set; }
}

使用此代碼

class ApiControl
{
    public void GetAllUsers()   
    {
        using (var client = new WebClient())
        {
            client.Headers.Add("Content-Type:application/json");
            client.Headers.Add("Accept:application/json");

            var result = client.DownloadString("https://api.hubapi.com/contacts/v1/lists/recently_updated/contacts/recent?hapikey=demo&count=2");

            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            dynamic dobj = jsonSerializer.Deserialize<dynamic>(result);
            var obf = dobj["contacts"]; 

            foreach (var item in obf)
            {
                var exampleModel = jsonSerializer.Deserialize<UserForDeserialize>(json);
            }
            Console.Read();
        }
    }
}

Id 填寫良好,但屬性列表具有所有 null 屬性。 也許是因為每個屬性中的“值”字段,但我找不到好的解決方案。 我可以嘗試反序列化 JSON 嗎?

你的 class 應該是這樣的。

public class Root
{
    public long addedAt { get; set; }
    public int vid { get; set; }
    public int canonicalvid { get; set; }
    public object[] mergedvids { get; set; }
    public int portalid { get; set; }
    public bool iscontact { get; set; }
    public string profiletoken { get; set; }
    public string profileurl { get; set; }
    public Properties properties { get; set; }
}

public class Properties
{
    public Firstname firstname { get; set; }
    public Lastmodifieddate lastmodifieddate { get; set; }
    public Company company { get; set; }
    public Lastname lastname { get; set; }
}

public class Firstname
{
    public string value { get; set; }
}

public class Lastmodifieddate
{
    public string value { get; set; }
}

public class Company
{
    public string value { get; set; }
}

public class Lastname
{
    public string value { get; set; }
}

然后使用它反序列化。

JavaScriptSerializer js = new JavaScriptSerializer();  
Root rootObject = js.Deserialize<Root>(result );  

看起來從 json 生成的 model 不正確。 我已經使用json2csharp來轉換您的 json 和 model 如下所示

public class Firstname
{
    public string value { get; set; }
}

public class Lastmodifieddate
{
    public string value { get; set; }
}

public class Company
{
    public string value { get; set; }
}

public class Lastname
{
    public string value { get; set; }
}

public class Properties
{
    public Firstname firstname { get; set; }
    public Lastmodifieddate lastmodifieddate { get; set; }
    public Company company { get; set; }
    public Lastname lastname { get; set; }
}

public class UserForDeserialize
{
    public int vid { get; set; }
    public Properties properties { get; set; }
}

在此之后,我能夠讀取屬性

using (StreamReader r = new StreamReader(filename))
{
   string json = r.ReadToEnd();
   var obj = JsonConvert.DeserializeObject<UserForDeserialize>(json);
   Console.WriteLine(obj.properties.firstname);
}

為另一個 class 更改“UserProperties”中的屬性類型,其中包含:

public string value { get; set; }

因為您在 UserProperties object 中缺少該屬性,所以我認為這是您要處理的主要問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM