简体   繁体   English

如何将具有多个对象的Json结果转换/反序列化为c#类,以及如何读取相同的内容?

[英]How to convert/deserialize Json result with multiple objects into c# classes and how we can read the same?

I am consuming wcf rest service which is returning json as output. 我正在使用wcf rest服务,该服务将json作为输出返回。

{  
"VerifyEmailResult":{  
  "EmailQueryResult":{  
     "query":{  
        "count":1,
        "created":"2014-04-23T08:38:04Z",
        "email":"test12%40yahoo.com",
        "lang":"en-US",
        "queryType":"EmailAgeVerification",
        "responseCount":0,
        "results":[  

        ]
     },
     "responseStatus":{  
        "description":"Authentication Error: The signature doesn’t match or the user\/consumer key file wasn’t found.",
        "errorCode":3001,
        "status":"failed"
     }
  },
  "Message":"Error occurred",
  "ResponseMessage":"Failure",
  "ResultCode":"0"
}
}

How can I deserialize the same. 我如何反序列化相同。 I don't have any class for json response. 我没有用于json响应的任何类。

I have to read json and to display some data from json. 我必须阅读json并显示json中的一些数据。

Thanks 谢谢

Here are your classes: 这是您的课程:

public class Query
{
    public int count { get; set; }
    public string created { get; set; }
    public string email { get; set; }
    public string lang { get; set; }
    public string queryType { get; set; }
    public int responseCount { get; set; }
    public List<object> results { get; set; }
}

public class ResponseStatus
{
    public string description { get; set; }
    public int errorCode { get; set; }
    public string status { get; set; }
}

public class EmailQueryResult
{
    public Query query { get; set; }
    public ResponseStatus responseStatus { get; set; }
}

public class VerifyEmailResult
{
    public EmailQueryResult EmailQueryResult { get; set; }
    public string Message { get; set; }
    public string ResponseMessage { get; set; }
    public string ResultCode { get; set; }
}

public class RootObject
{
    public VerifyEmailResult VerifyEmailResult { get; set; }
}

you can use JSON2Csharp.com to get generated classes for you Json in C# . 您可以使用JSON2Csharp.comC#中Json获取生成的类。

Use Newton Soft Json library to Deserialize json. 使用Newton Soft Json库反序列化json。

you can Deserialise using this method of library: 您可以使用以下库方法反序列化:

StreamReader reader = new StreamReader(response.GetResponseStream());

string json = reader.ReadToEnd();


var Jsonobject = JsonConvert.DeserializeObject<RootObject>(json);// pass your string json here

VerifyEmailResult result = Jsonobject.VerifyEmailResult ;

In my case i was sending a web request to a Restful service and json was returning as string. 就我而言,我正在将Web请求发送到Restful服务,而json作为字符串返回。

How can I deserialize the same. 我如何反序列化相同。 I don't have any class for json response. 我没有用于json响应的任何类。

If you don't have classes for json string, You can deserialize to dynamic object at runtime. 如果您没有json字符串类,则可以在运行时反序列化为动态对象。

Example: 例:

        dynamic Jsonobject = JsonConvert.DeserializeObject<dynamic>(json);
        Console.WriteLine(Jsonobject.VerifyEmailResult.EmailQueryResult.query.email);
        Console.WriteLine(Jsonobject.VerifyEmailResult.EmailQueryResult.query["lang"]);

Try it online 在线尝试

Output: 输出:

test12@yahoo.com test12@yahoo.com

en-US EN-US

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

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