反序列化JSON返回空数据

反序列化JSON返回空数据,json,datacontractjsonserializer,Json,Datacontractjsonserializer,我使用api url获取json响应 http://localhost/WaWebService/Json/NodeDetail/Demo/SCADA_NODE_DEMO 使用Postman软件,我验证是否有响应 { "Result": { "Ret": 0 }, "Node": { "ProjectId": 1, "NodeId": 1, "NodeName": "SCADA_NODE_DEMO",

我使用api url获取json响应

http://localhost/WaWebService/Json/NodeDetail/Demo/SCADA_NODE_DEMO
使用
Postman
软件,我验证是否有响应

{
    "Result": {
        "Ret": 0
    },
    "Node": {
        "ProjectId": 1,
        "NodeId": 1,
        "NodeName": "SCADA_NODE_DEMO",
        "Description": "",
        "Address": "SALMAN-MUSHTAQ",
        "Port1": 0,
        "Port2": 0,
        "Timeout": 0
    }
}
课后我上课

class Result
    {
        public int Ret { get; set; }
    }

    public class Node
    {
        public int ProjectId { get; set; }
        public int NodeId { get; set; }
        public string NodeName { get; set; }
        public string Description { get; set; }
        public string Address { get; set; }
        public int Port1 { get; set; }
        public int Port2 { get; set; }
        public int Timeout { get; set; }
    }
现在,我使用
DataContractJsonSerializer

var client = new WebClient { Credentials = new NetworkCredential("username", "password") };


                string json = client.DownloadString(url);
                using(var ms = new MemoryStream (Encoding.Unicode.GetBytes(json)))
                {
                    DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(Node));
                    Node nObj = (Node)deserializer.ReadObject(ms);
                    Console.WriteLine("Node name: " + nObj.NodeName);
                }

它在控制台上没有显示任何内容。请帮助解决这个问题。提前感谢。

您应该创建与响应json具有相同结构的类

class JsonResponse
{
    public Result Result { get; set; }
    public Node Node { get; set; }
}

class Result
{
    public int Ret { get; set; }
}

public class Node
{
    public int ProjectId { get; set; }
    public int NodeId { get; set; }
    public string NodeName { get; set; }
    public string Description { get; set; }
    public string Address { get; set; }
    public int Port1 { get; set; }
    public int Port2 { get; set; }
    public int Timeout { get; set; }
}
然后像这样反序列化json

DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(JsonResponse)); 

我也尝试使用Json.Net和JavaScriptSerializer,但遇到了相同的问题。请帮助专家。