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

Emm*_*uel 21 .net c# json.net

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

    {
    "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...
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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

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...
}
Run Code Online (Sandbox Code Playgroud)

我想最终得到这个:

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

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

Jon*_*eet 29

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

var result = rates.ToDictionary(pair => pair.Key, pair => (decimal) pair.Value);
Run Code Online (Sandbox Code Playgroud)

不幸的是,它使用显式接口实现,这使得这有点痛苦 - 但如果你通过IDictionary<string, JToken>接口,它没关系.

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

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"]);
    }
}
Run Code Online (Sandbox Code Playgroud)


Adr*_*lid 9

这对你有用吗?

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;

namespace JsonNetTest
{



    class Program
    {
        static void Main(string[] args)
        {

            string jsonString = @"{
                '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
                }
            }";

            JObject parsed = JObject.Parse(jsonString);

            Dictionary<string, decimal> rates = parsed["rates"].ToObject<Dictionary<string, decimal>>();

            Console.WriteLine(rates["ALL"]);

            Console.ReadKey();

        }
    }
}
Run Code Online (Sandbox Code Playgroud)