0

我之前已经成功使用 Newtonsoft.Json 反序列化 JSON,但我遇到了这个简单示例的问题 - 请参见下文 - 反序列化方法不会失败,但它不会使用预期值实例化“ServiceResponse”类。调试器显示:ServiceResponse.StatusInfo = null,ServiceResponse.Email = null,ServiceResponse.JobId = 0

// Generated by Xamasoft JSON Class Generator
// http://www.xamasoft.com/json-class-generator

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Example.fmeResponseJsonTypes;

namespace Example.fmeResponseJsonTypes
{

    public class StatusInfo
    {

        [JsonProperty("mode")]
        public string Mode { get; set; }

        [JsonProperty("status")]
        public string Status { get; set; }
    }

    public class ServiceResponse
    {

        [JsonProperty("statusInfo")]
        public StatusInfo StatusInfo { get; set; }

        [JsonProperty("email")]
        public string Email { get; set; }

       [JsonProperty("jobID")]
        public int JobID { get; set; }
    }

}

namespace Example
{

    public class fmeResponse
    {

        [JsonProperty("serviceResponse")]
        public ServiceResponse ServiceResponse { get; set; }

        public void Deserialize()
        {
           string res = "{\"serviceResponse\": {\"statusInfo\": {\"mode\": \"async\",\"status\": \"success\"},\"email\": \"tor.nielsen@xxx.com\",\"jobID\": 73}}";

           ServiceResponse serviceResponse = null;

           try
           {
               serviceResponse = JsonConvert.DeserializeObject<ServiceResponse>(res);
           }
           catch (Exception e)
           {
               throw new Exception("Error: Deserialization of [" + res + "] failed! \nDetails: " + e.Message + "\nTrace: " + e.StackTrace);
           }

        }

    }

}
4

1 回答 1

0

试试这个,请添加 JsonProperty ,我很着急。

如果您不确定 JsonString 所需的类,请尝试json2csharp。它有时真的很有帮助。

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Example.fmeResponseJsonTypes;

namespace Example.fmeResponseJsonTypes
{
public class StatusInfo
{
    public string mode { get; set; }
    public string status { get; set; }
}

public class ServiceResponse
{
    public StatusInfo statusInfo { get; set; }
    public string email { get; set; }
    public int jobID { get; set; }
}

public class RootObject
{
    public ServiceResponse serviceResponse { get; set; }
}

}

namespace Example
{

public class fmeResponse
{

    public RootObject RootObject { get; set; }

    public void Deserialize()
    {
        string res = "{\"serviceResponse\": {\"statusInfo\": {\"mode\": \"async\",\"status\": \"success\"},\"email\": \"tor.nielsen@xxx.com\",\"jobID\": 73}}";

        ServiceResponse serviceResponse = null;
        RootObject rootObject = null;

        try
        {
            rootObject = JsonConvert.DeserializeObject<RootObject>(res);
        }
        catch (Exception e)
        {
            throw new Exception("Error: Deserialization of [" + res + "] failed! \nDetails: " + e.Message + "\nTrace: " + e.StackTrace);
        }
    }
}
于 2013-04-06T12:53:04.557 回答