<bdo id='xSFs6'></bdo><ul id='xSFs6'></ul>
<legend id='xSFs6'><style id='xSFs6'><dir id='xSFs6'><q id='xSFs6'></q></dir></style></legend>
  • <i id='xSFs6'><tr id='xSFs6'><dt id='xSFs6'><q id='xSFs6'><span id='xSFs6'><b id='xSFs6'><form id='xSFs6'><ins id='xSFs6'></ins><ul id='xSFs6'></ul><sub id='xSFs6'></sub></form><legend id='xSFs6'></legend><bdo id='xSFs6'><pre id='xSFs6'><center id='xSFs6'></center></pre></bdo></b><th id='xSFs6'></th></span></q></dt></tr></i><div id='xSFs6'><tfoot id='xSFs6'></tfoot><dl id='xSFs6'><fieldset id='xSFs6'></fieldset></dl></div>

      <tfoot id='xSFs6'></tfoot>
    1. <small id='xSFs6'></small><noframes id='xSFs6'>

        JObject.Parse 与 JsonConvert.DeserializeObject

        时间:2023-08-22

              <bdo id='1UkEh'></bdo><ul id='1UkEh'></ul>
            • <legend id='1UkEh'><style id='1UkEh'><dir id='1UkEh'><q id='1UkEh'></q></dir></style></legend><tfoot id='1UkEh'></tfoot>
              <i id='1UkEh'><tr id='1UkEh'><dt id='1UkEh'><q id='1UkEh'><span id='1UkEh'><b id='1UkEh'><form id='1UkEh'><ins id='1UkEh'></ins><ul id='1UkEh'></ul><sub id='1UkEh'></sub></form><legend id='1UkEh'></legend><bdo id='1UkEh'><pre id='1UkEh'><center id='1UkEh'></center></pre></bdo></b><th id='1UkEh'></th></span></q></dt></tr></i><div id='1UkEh'><tfoot id='1UkEh'></tfoot><dl id='1UkEh'><fieldset id='1UkEh'></fieldset></dl></div>

              <small id='1UkEh'></small><noframes id='1UkEh'>

                <tbody id='1UkEh'></tbody>

                  本文介绍了JObject.Parse 与 JsonConvert.DeserializeObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  JsonConvert.DeserializeObject 和 JObject.Parse 有什么区别?据我所知,两者都采用字符串并且位于 Json.NET 库中.什么样的情况会让一种比另一种更方便,还是主要只是偏好?

                  What's the difference between JsonConvert.DeserializeObject and JObject.Parse? As far as I can tell, both take a string and are in the Json.NET library. What kind of situation would make one more convenient than the other, or is it mainly just preference?

                  作为参考,这里有一个我使用两者来做完全相同的事情的例子——解析一个 Json 字符串并返回一个 Json 属性列表.

                  For reference, here's an example of me using both to do exactly the same thing - parse a Json string and return a list of one of the Json attributes.

                  public ActionResult ReadJson()
                  {
                      string countiesJson = "{'Everything':[{'county_name':null,'description':null,'feat_class':'Civil','feature_id':'36865',"
                                      +"'fips_class':'H1','fips_county_cd':'1','full_county_name':null,'link_title':null,'url':'http://www.alachuacounty.us/','name':'Alachua County'"+ ",'primary_latitude':'29.7','primary_longitude':'-82.33','state_abbreviation':'FL','state_name':'Florida'},"+
                                      "{'county_name':null,'description':null,"+ "'feat_class':'Civil','feature_id':'36866','fips_class':'H1','fips_county_cd':'3','full_county_name':null,'link_title':null,'url':'http://www.bakercountyfl.org/','name':'Baker County','primary_latitude':'30.33','primary_longitude':'-82.29','state_abbreviation':'FL','state_name':'Florida'}]}";
                  
                      //Can use either JSONParseObject or JSONParseDynamic here
                      List<string> counties = JSONParseObject(countiesJson);
                      JSONParseDynamic(countiesJson);
                      return View(counties);
                  }
                  
                  public List<string> JSONParseObject(string jsonText)
                  {
                      JObject jResults = JObject.Parse(jsonText);
                      List<string> counties = new List<string>();
                      foreach (var county in jResults["Everything"])
                      {
                          counties.Add((string)county["name"]);
                      }
                      return counties;
                  }
                  
                  public List<string> JSONParseDynamic(string jsonText)
                  {
                      dynamic jResults = JsonConvert.DeserializeObject(jsonText);
                      List<string> counties = new List<string>();
                      foreach(var county in jResults.Everything)
                      {
                          counties.Add((string)county.name);
                      }
                      return counties;
                  }
                  

                  推荐答案

                  存在 LINQ-to-JSON API(JObjectJToken 等)以允许工作使用 JSON,无需提前知道其结构.您可以使用 JToken.Parse 反序列化任意 JSON,然后使用其他 JToken 方法检查和操作其内容.如果您只需要 JSON 中的一两个值(例如县名),LINQ-to-JSON 也可以很好地工作.

                  The LINQ-to-JSON API (JObject, JToken, etc.) exists to allow working with JSON without needing to know its structure ahead of time. You can deserialize any arbitrary JSON using JToken.Parse, then examine and manipulate its contents using other JToken methods. LINQ-to-JSON also works well if you just need one or two values from the JSON (such as the name of a county).

                  JsonConvert.DeserializeObject 主要用于当您提前知道 JSON 的结构并且想要反序列化为强类型类时使用.例如,您可以通过以下方式将完整的县数据集从 JSON 中获取到 County 对象列表中.

                  JsonConvert.DeserializeObject, on the other hand, is mainly intended to be used when you DO know the structure of the JSON ahead of time and you want to deserialize into strongly typed classes. For example, here's how you would get the full set of county data from your JSON into a list of County objects.

                  class Program
                  {
                      static void Main(string[] args)
                      {
                          string countiesJson = "{'Everything':[{'county_name':null,'description':null,'feat_class':'Civil','feature_id':'36865',"
                                  +"'fips_class':'H1','fips_county_cd':'1','full_county_name':null,'link_title':null,'url':'http://www.alachuacounty.us/','name':'Alachua County'"+ ",'primary_latitude':'29.7','primary_longitude':'-82.33','state_abbreviation':'FL','state_name':'Florida'},"+
                                  "{'county_name':null,'description':null,"+ "'feat_class':'Civil','feature_id':'36866','fips_class':'H1','fips_county_cd':'3','full_county_name':null,'link_title':null,'url':'http://www.bakercountyfl.org/','name':'Baker County','primary_latitude':'30.33','primary_longitude':'-82.29','state_abbreviation':'FL','state_name':'Florida'}]}";
                  
                          foreach (County c in JsonParseCounties(countiesJson))
                          {
                              Console.WriteLine(string.Format("{0}, {1} ({2},{3})", c.name, 
                                 c.state_abbreviation, c.primary_latitude, c.primary_longitude));
                          }
                      }
                  
                      public static List<County> JsonParseCounties(string jsonText)
                      {
                          return JsonConvert.DeserializeObject<RootObject>(jsonText).Counties;
                      }
                  }
                  
                  public class RootObject
                  {
                      [JsonProperty("Everything")]
                      public List<County> Counties { get; set; }
                  }
                  
                  public class County
                  {
                      public string county_name { get; set; }
                      public string description { get; set; }
                      public string feat_class { get; set; }
                      public string feature_id { get; set; }
                      public string fips_class { get; set; }
                      public string fips_county_cd { get; set; }
                      public string full_county_name { get; set; }
                      public string link_title { get; set; }
                      public string url { get; set; }
                      public string name { get; set; }
                      public string primary_latitude { get; set; }
                      public string primary_longitude { get; set; }
                      public string state_abbreviation { get; set; }
                      public string state_name { get; set; }
                  }
                  

                  请注意,Json.Net 使用 JsonConvert.DeserializeObject 方法的类型参数来确定要创建的对象类型.

                  Notice that Json.Net uses the type argument given to the JsonConvert.DeserializeObject method to determine what type of object to create.

                  当然,如果你调用DeserializeObject时没有指定类型,或者你使用objectdynamic,那么Json.Net别无选择,只能反序列化为JObject.(您可以通过检查 jResults.GetType().FullName 亲自看到您的动态变量实际上包含一个 JObject.)所以在这种情况下,<代码>JsonConvert.DeserializeObject 和 JToken.Parse;两者都会给你同样的结果.

                  Of course, if you don't specify a type when you call DeserializeObject, or you use object or dynamic, then Json.Net has no choice but to deserialize into a JObject. (You can see for yourself that your dynamic variable actually holds a JObject by checking jResults.GetType().FullName.) So in that case, there's not much difference between JsonConvert.DeserializeObject and JToken.Parse; either will give you the same result.

                  这篇关于JObject.Parse 与 JsonConvert.DeserializeObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将两个 JToken 合并为一个 下一篇:检查 JObject 中的空或空 JToken

                  相关文章

                  最新文章

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

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

                  1. <legend id='ACwnX'><style id='ACwnX'><dir id='ACwnX'><q id='ACwnX'></q></dir></style></legend>
                        <bdo id='ACwnX'></bdo><ul id='ACwnX'></ul>