简体   繁体   English

Newtonsoft JSON DeserializeObject序列化对象的反序列化

[英]Newtonsoft JSON DeserializeObject Deserialization of Serialized object

I have the following JSON string being returned that I am trying to deserialize, but I am having problems with it filling in the values for the properties on the object. 我返回了以下要反序列化的JSON字符串,但是在填写对象属性值时遇到问题。

Here is the JSON string being returned back: 这是返回的JSON字符串:

"{\"ClientData\":[{\"clientID\":9999999,\"userID\":123,\"authID\":\"8a20627be9ec4c608f4c609a24e74174\"}]}"

Now, I have my ClientData class specified via the following: 现在,我通过以下方法指定了ClientData类:

public class ClientData
{
    [JsonProperty("clientID")]
    public long? ClientID { get; set; }

    [JsonProperty("userID")]
    public int? UserID { get; set; }

    [JsonProperty("authID")]
    public string AuthID { get; set; }
}

What am I doing wrong here in the conversion? 转换中我在做什么错? I thought it would be as simple as specifying: 我以为只要指定就可以了:

var result = JsonConvert.DeserializeObject<ClientData>(auth);

However, this doesn't yield any results. 但是,这不会产生任何结果。 I think the problem may be how the JSON string is being constructed, ie the properties defined for the object. 我认为问题可能在于如何构造JSON字符串,即为对象定义的属性。

Can someone help point me in the right direction? 有人可以帮我指出正确的方向吗?

Thanks 谢谢

The data you have is a JSON array wrapped inside another object. 您拥有的数据是包装在另一个对象内的JSON数组。 You need to create a secondary class, place your ClientData as a list inside that one and deserialize that it. 您需要创建一个辅助类,将ClientData作为列表放置在该类中,然后对其进行反序列化。

Base class that contains your list 包含您的列表的基类

public class ClientDataInformation 
{
    [JsonProperty("ClientData")]
    public List<ClientData> ClientList {get;set;}
}

Deserialization 反序列化

var result = JsonConvert.DeserializeObject<ClientDataInformation>(auth);

This might solve the problem 这可能会解决问题

private static clientFileName = "yourfile.dat";

public static async Task<List<ClientData>> LoadClientDataFromJsonAsync()
    {
        string clientJsonString = await DeserializeClientDataAsync(clientFileName);
        if (clientJsonString != null)
            return (List<ClientData>)JsonConvert.DeserializeObject(clientJsonString, typeof(List<ClientData>));
        return null;
    }

private static async Task<ClientData> DeserializeClientDataAsync(string fileName)
    {
        try
        {
            StorageFile localFile = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
            return await FileIO.ReadTextAsync(localFile);
        }
        catch (FileNotFoundException)
        {
            return null;
        }

Looks like your client data is set as an array. 看起来您的客户数据已设置为数组。 Remove the brackets and see if that works. 卸下支架,看看是否可行。 Also, you have it wrapped in another class. 另外,您将其包装在另一个类中。 If you need the prefaced ClientData, you'll need some kind of class like: 如果需要开头的ClientData,则需要类似以下的类:

public class ClientDataObject
{
    public ClientData ClientData { get; set; }
}

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

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