久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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 class="qwawimqqmiuu" 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>
  1. <small id='8zMYW'></small><noframes id='8zMYW'>

      <tfoot id='8zMYW'></tfoot>

      將 JSON.NET JObject 的屬性/標記轉換為字典鍵

      Converting a JSON.NET JObject#39;s Properties/Tokens into Dictionary Keys(將 JSON.NET JObject 的屬性/標記轉換為字典鍵)
        <bdo id='KhgQ6'></bdo><ul id='KhgQ6'></ul>
            <tbody id='KhgQ6'></tbody>

            <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 class="qwawimqqmiuu" id='KhgQ6'><tfoot id='KhgQ6'></tfoot><dl id='KhgQ6'><fieldset id='KhgQ6'></fieldset></dl></div>
                本文介紹了將 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模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                Parse malformed XML(解析格式錯誤的 XML)
                • <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'>

                      <tbody id='iknKj'></tbody>
                    <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 class="qwawimqqmiuu" id='iknKj'><tfoot id='iknKj'></tfoot><dl id='iknKj'><fieldset id='iknKj'></fieldset></dl></div>

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

                        1. 主站蜘蛛池模板: 天堂亚洲 | 国内精品久久久久 | 99国产精品99久久久久久 | xxxcom在线观看 | 99色综合| 91免费入口 | 国产精品夜色一区二区三区 | 中文成人无字幕乱码精品 | 国产99久久精品 | 天堂中文av| 欧美一区二区在线观看 | 91一区| 国产999精品久久久久久 | 伊人超碰在线 | 国产精品成人一区二区三区吃奶 | 一区二区三区四区免费观看 | 精品中文字幕视频 | 午夜精品一区 | 日本在线视频一区二区 | 久久久精品网 | 亚洲国产精品美女 | 日日射影院| 日韩电影免费在线观看中文字幕 | 成人精品一区二区三区中文字幕 | 久久免费高清视频 | 男人天堂网址 | 天堂一区二区三区 | 在线欧美| 国产精品精品久久久 | 中文字幕一二三区 | 日韩高清成人 | 欧美日韩精品中文字幕 | 中文字幕第一页在线 | 国外成人在线视频 | 国产精品毛片一区二区三区 | 亚洲精品乱码久久久久久蜜桃 | 国产一级淫片a直接免费看 免费a网站 | 欧美小视频在线观看 | 日韩黄色小视频 | www.毛片| 亚洲一区二区在线视频 |