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

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

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

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

        将 JSON.NET JObject 的属性/标记转换为字典键

        时间:2023-08-23

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

            <legend id='8zMYW'><style id='8zMYW'><dir id='8zMYW'><q id='8zMYW'></q></dir></style></legend>
              • <bdo id='8zMYW'></bdo><ul id='8zMYW'></ul>
              • <small id='8zMYW'></small><noframes id='8zMYW'>

                    <tbody id='8zMYW'></tbody>
                  <tfoot id='8zMYW'></tfoot>
                  本文介绍了将 JSON.NET JObject 的属性/标记转换为字典键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 JSON.NET 来解析来自 openexhangerates.org 服务器端的 JSON 响应.响应包含一个嵌套对象(rates"),其中包含一长串数字属性:

                  I'm using JSON.NET to parse a JSON reponse from openexhangerates.org server side using .NET. The response contains a nested object ("rates") which has a long list of numeric properties:

                      {
                      "disclaimer": "Exchange rates provided for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability, or fitness for any purpose; use at your own risk. Other than that, have fun! Usage subject to acceptance of terms: http://openexchangerates.org/terms/",
                      "license": "Data sourced from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given. Usage subject to acceptance of license agreement: http://openexchangerates.org/license/",
                          "timestamp": 1357268408,
                          "base": "USD",
                          "rates": {
                              "AED": 3.673033,
                              "AFN": 51.5663,
                              "ALL": 106.813749,
                              "AMD": 403.579996,
                              etc...
                          }
                      }
                  

                  属性名称对应于货币类型(例如USD").我需要假设属性列表会随着时间而改变,所以我想将对象转换为 Dictionary 而不是对应的 C# 对象.

                  The property names correspond to the currency type (e.g. "USD"). I need to assume that the list of properties can change over time, so I want to convert the object into a Dictionary instead of a corresponding C# object.

                  所以不要将 JSON 对象反序列化成这样的东西:

                  So instead of deserializing the JSON object into something like this:

                  class Rates
                  {
                  public decimal AED; // United Arab Emirates Dirham
                  public decimal AFN; // Afghan Afghani
                  public decimal ALL; // Albanian Lek
                  public decimal AMD; // Armenian Dram
                  // etc...
                  }
                  

                  我想这样结束:

                  Dictionary<string,decimal>() {{"AED",0.2828},{"AFN",0.3373},{"ALL",2.2823},{"AMD",33.378} // etc...};
                  

                  如何从响应字符串或调用 JObject.Parse(responseString) 生成的 JObject 开始?

                  How do I do this starting from either the response string or from the JObject produced by calling JObject.Parse(responseString)?

                  推荐答案

                  JObject 已经实现了 IDictionary,所以我怀疑当你导航到 rates 成员,您应该可以使用:

                  JObject already implements IDictionary<string, JToken>, so I suspect that when you've navigated down to the rates member, you should be able to use:

                  var result = rates.ToDictionary(pair => pair.Key, pair => (decimal) pair.Value);
                  

                  不幸的是,它使用显式接口实现,这让这有点痛苦 - 但如果你通过 IDictionary 接口,那就没问题了.

                  Unfortunately it uses explicit interface implementation, which makes this a bit of a pain - but if you go via the IDictionary<string, JToken> interface, it's fine.

                  这是一个简短但完整的示例,它似乎适用于您提供的 JSON(保存到 test.json 文件中):

                  Here's a short but complete example which appears to work with the JSON you've provided (saved into a test.json file):

                  using System;
                  using System.Collections.Generic;
                  using System.IO;
                  using System.Linq;
                  using Newtonsoft.Json.Linq;
                  
                  class Test
                  {
                      static void Main()
                      {
                          JObject parsed = JObject.Parse(File.ReadAllText("test.json"));
                          IDictionary<string, JToken> rates = (JObject) parsed["rates"];
                          // Explicit typing just for "proof" here
                          Dictionary<string, decimal> dictionary =
                              rates.ToDictionary(pair => pair.Key,
                                                 pair => (decimal) pair.Value);
                          Console.WriteLine(dictionary["ALL"]);
                      }
                  }
                  

                  这篇关于将 JSON.NET JObject 的属性/标记转换为字典键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Json.Net PopulateObject 追加列表而不是设置值 下一篇:json.net 不序列化派生类的属性

                  相关文章

                  最新文章

                1. <legend id='iknKj'><style id='iknKj'><dir id='iknKj'><q id='iknKj'></q></dir></style></legend>
                  <tfoot id='iknKj'></tfoot>

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

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

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