• <tfoot id='mVRlQ'></tfoot>

      <bdo id='mVRlQ'></bdo><ul id='mVRlQ'></ul>

    <small id='mVRlQ'></small><noframes id='mVRlQ'>

    <i id='mVRlQ'><tr id='mVRlQ'><dt id='mVRlQ'><q id='mVRlQ'><span id='mVRlQ'><b id='mVRlQ'><form id='mVRlQ'><ins id='mVRlQ'></ins><ul id='mVRlQ'></ul><sub id='mVRlQ'></sub></form><legend id='mVRlQ'></legend><bdo id='mVRlQ'><pre id='mVRlQ'><center id='mVRlQ'></center></pre></bdo></b><th id='mVRlQ'></th></span></q></dt></tr></i><div id='mVRlQ'><tfoot id='mVRlQ'></tfoot><dl id='mVRlQ'><fieldset id='mVRlQ'></fieldset></dl></div>
      1. <legend id='mVRlQ'><style id='mVRlQ'><dir id='mVRlQ'><q id='mVRlQ'></q></dir></style></legend>
      2. JSON.NET 反序列化特定属性

        时间:2023-08-22
        <i id='we7m9'><tr id='we7m9'><dt id='we7m9'><q id='we7m9'><span id='we7m9'><b id='we7m9'><form id='we7m9'><ins id='we7m9'></ins><ul id='we7m9'></ul><sub id='we7m9'></sub></form><legend id='we7m9'></legend><bdo id='we7m9'><pre id='we7m9'><center id='we7m9'></center></pre></bdo></b><th id='we7m9'></th></span></q></dt></tr></i><div id='we7m9'><tfoot id='we7m9'></tfoot><dl id='we7m9'><fieldset id='we7m9'></fieldset></dl></div>
          <bdo id='we7m9'></bdo><ul id='we7m9'></ul>
        • <legend id='we7m9'><style id='we7m9'><dir id='we7m9'><q id='we7m9'></q></dir></style></legend>
          1. <small id='we7m9'></small><noframes id='we7m9'>

                    <tbody id='we7m9'></tbody>
                  <tfoot id='we7m9'></tfoot>
                1. 本文介绍了JSON.NET 反序列化特定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下 JSON 文本:

                  {
                      "PropOne": {
                          "Text": "Data"
                      }
                      "PropTwo": "Data2"
                  }    
                  

                  我想将 PropOne 反序列化为 PropOneClass 类型,而无需反序列化对象上的任何其他属性.这可以使用 JSON.NET 完成吗?

                  I want to deserialize PropOne into type PropOneClass without the overhead of deserializing any other properties on the object. Can this be done using JSON.NET?

                  推荐答案

                  public T GetFirstInstance<T>(string propertyName, string json)
                  {
                      using (var stringReader = new StringReader(json))
                      using (var jsonReader = new JsonTextReader(stringReader))
                      {
                          while (jsonReader.Read())
                          {
                              if (jsonReader.TokenType == JsonToken.PropertyName
                                  && (string)jsonReader.Value == propertyName)
                              {
                                  jsonReader.Read();
                  
                                  var serializer = new JsonSerializer();
                                  return serializer.Deserialize<T>(jsonReader);
                              }
                          }
                          return default(T);
                      }
                  }
                  
                  public class MyType
                  {
                      public string Text { get; set; }
                  }
                  
                  public void Test()
                  {
                      string json = "{ "PropOne": { "Text": "Data" }, "PropTwo": "Data2" }";
                  
                      MyType myType = GetFirstInstance<MyType>("PropOne", json);
                  
                      Debug.WriteLine(myType.Text);  // "Data"
                  }
                  

                  这种方法避免了必须反序列化整个对象.但请注意,只有当 json 显着很大并且您要反序列化的属性在数据中相对较早时,这才会提高性能.否则,你应该反序列化整个事情并取出你想要的部分,比如 jcwrequests 回答节目.

                  This approach avoids having to deserialize the entire object. But note that this will only improve performance if the json is significantly large, and the property you are deserializing is relatively early in the data. Otherwise, you should just deserialize the whole thing and pull out the parts you want, like jcwrequests answer shows.

                  这篇关于JSON.NET 反序列化特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 JSON 数组反序列化为强类型的 .NET 对象 下一篇:如何返回 JSON 对象

                  相关文章

                  最新文章

                    • <bdo id='BCrWk'></bdo><ul id='BCrWk'></ul>

                  1. <small id='BCrWk'></small><noframes id='BCrWk'>

                    <legend id='BCrWk'><style id='BCrWk'><dir id='BCrWk'><q id='BCrWk'></q></dir></style></legend>

                  2. <tfoot id='BCrWk'></tfoot>
                  3. <i id='BCrWk'><tr id='BCrWk'><dt id='BCrWk'><q id='BCrWk'><span id='BCrWk'><b id='BCrWk'><form id='BCrWk'><ins id='BCrWk'></ins><ul id='BCrWk'></ul><sub id='BCrWk'></sub></form><legend id='BCrWk'></legend><bdo id='BCrWk'><pre id='BCrWk'><center id='BCrWk'></center></pre></bdo></b><th id='BCrWk'></th></span></q></dt></tr></i><div id='BCrWk'><tfoot id='BCrWk'></tfoot><dl id='BCrWk'><fieldset id='BCrWk'></fieldset></dl></div>