Json   发布时间:2022-04-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用JsonSerializer选择性地读取部分JSON数据并填充c#对象大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我连接到第三方Web服务,该服务返回一个复杂的 JSON对象,该对象仅包含我实际需要的一些信息.

基本上,我只需要“值”中的数组.从该数组中,我只需要“Id”,“title”和“Status”属性.

我想将这些属性放入名为Project的c#类中.这是我的班级:

public class Project
{
    public String Id { get; set; }
    public String title { get; set; }
    public String Status { get; set; }
}

我正在尝试使用此代码来读取JSON并执行转换:

using (WebResponse response = request.GetResponse())
{
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        var serializer = new Jsonserializer();
        var jsontextReader = new JsontextReader(reader);
        returnValue = serializer.Deserialize<Project>(jsontextReader);
    }
}

示例JSON:

{
    "odata.Metadata":"http://school.edu/Api/1/$Metadata#Projects","odata.count":"3","value":[
        {
            "odata.id":"http://school.edu/Api/1/Projects('123')","RelatedProjects@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('123')/RelatedProjects","Tags@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('123')/Tags","TimedEvents@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('123')/Categories","ep@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('123')/ep","#CreateLike":{
                    "target":"http://school.edu/Api/1/Projects('123')/CreateLike"
                  },"#CreateShortcut":{
                        "target":"http://school.edu/Api/1/Projects('123')/CreateShortcut"
                      },"#Play":{
                        "target":"http://school.edu/Play/123"
                      },"#SendInvitation":{
                        "target":"http://school.edu/Api/1/Projects('123')/SendInvitation"
                      },"#CopyProject":{
                        "target":"http://school.edu/Api/1/Projects('123')/CopyProject"
                      },"#AddVideopodcast":{
                        "target":"http://school.edu/Api/1/Projects('123')/AddVideopodcast"
                      },"#AddEP":{
                        "target":"http://school.edu/Api/1/Projects('123')/AddEP"
                      },"Id":"123","title":"Test title 1","Status":"Viewable"
        },{
            "odata.id":"http://school.edu/Api/1/Projects('456')","RelatedProjects@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('456')/RelatedProjects","Tags@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('456')/Tags","TimedEvents@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('456')/Categories","ep@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('456')/ep","#CreateLike":{
                    "target":"http://school.edu/Api/1/Projects('456')/CreateLike"
                  },"#CreateShortcut":{
                        "target":"http://school.edu/Api/1/Projects('456')/CreateShortcut"
                      },"#Play":{
                        "target":"http://school.edu/Play/456"
                      },"#SendInvitation":{
                        "target":"http://school.edu/Api/1/Projects('456')/SendInvitation"
                      },"#CopyProject":{
                        "target":"http://school.edu/Api/1/Projects('456')/CopyProject"
                      },"#AddVideopodcast":{
                        "target":"http://school.edu/Api/1/Projects('456')/AddVideopodcast"
                      },"#AddEP":{
                        "target":"http://school.edu/Api/1/Projects('456')/AddEP"
                      },"Id":"456","title":"Test title 2",{
            "odata.id":"http://school.edu/Api/1/Projects('789')","RelatedProjects@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('789')/RelatedProjects","Tags@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('789')/Tags","TimedEvents@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('789')/Categories","ep@odata.navigationLinkUrl":"http://school.edu/Api/1/Projects('789')/ep","#CreateLike":{
                    "target":"http://school.edu/Api/1/Projects('789')/CreateLike"
                  },"#CreateShortcut":{
                        "target":"http://school.edu/Api/1/Projects('789')/CreateShortcut"
                      },"#Play":{
                        "target":"http://school.edu/Play/789"
                      },"#SendInvitation":{
                        "target":"http://school.edu/Api/1/Projects('789')/SendInvitation"
                      },"#CopyProject":{
                        "target":"http://school.edu/Api/1/Projects('789')/CopyProject"
                      },"#AddVideopodcast":{
                        "target":"http://school.edu/Api/1/Projects('789')/AddVideopodcast"
                      },"#AddEP":{
                        "target":"http://school.edu/Api/1/Projects('789')/AddEP"
                      },"Id":"789","title":"Test title 3","Status":"Viewable"
        }
  ],"odata.nextLink":"http://school.edu/Api/1/Folders('xyz')/Projects?$skip=10&$top=10"
}

我只是得到一个null对象.但是在调试器中,我可以看到它正在从Web服务中提取所有JSON数据.

如何从JSON获得我需要的东西,构建我的c#对象,并忽略所有其余的东西?

解决方法

如果你可以使用Json.NET(Newtonsoft json),你可以像这样利用Linq-to-Json [1]

//using Newtonsoft.Json.Linq;
var jsonString = File.ReadAllText(@"C:YourDirectory\file.json"); //source
var projects = new List<Project>(); //Your result

JObject data = JObject.Parse(jsonString);
foreach (var value in data["value"])
{
    projects.Add(new Project
    {
        Id = value["Id"].ToString(),Status = value["Status"].ToString(),title = value["title"].ToString()
    });
}

或者,您也可以像这样反序列化JObject [2]

var jsonReader = data["value"].CreateReader();
projects = new Jsonserializer().Deserialize<List<Project>>(jsonReader);

两者都很好,但哪一个更好?

第二种方法意味着更少的代码(特别是,如果在Project类中有许多属性,则必须编写许多代码行来映射代码[1]中的每个属性).

但第一种方法的表现要好很多倍!对于给定的json数据,代码[1]大约在1毫秒内运行,而代码[2]需要超过100毫秒!

更新

在James Newton-King(编写Json.NET :)的输入之后,还有另一种更优雅的方式来做到这一点[3]

projects = data["value"].ToObject<List<Project>>();

你猜怎么着!这段代码[3]花费了近一半的时间[1].因此,从各方面来看,这必须是最好的方法

大佬总结

以上是大佬教程为你收集整理的使用JsonSerializer选择性地读取部分JSON数据并填充c#对象全部内容,希望文章能够帮你解决使用JsonSerializer选择性地读取部分JSON数据并填充c#对象所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。