C# 从JSON查询获取值时遇到问题

C# 从JSON查询获取值时遇到问题,c#,json,C#,Json,我正在尝试从json api读取数据。下面是我的代码,我还包含了json数据的一段代码。在我的示例中,我试图获得当前的GBP转换率。我的代码在粗体的行中被卡住了。这是我第一次使用JSON,那么是否有一种简单的方法可以仅从该数据返回GBP值 public class Currency { public List<string> rates { get; set; } } public decimal convertCurrency(decima

我正在尝试从json api读取数据。下面是我的代码,我还包含了json数据的一段代码。在我的示例中,我试图获得当前的GBP转换率。我的代码在粗体的行中被卡住了。这是我第一次使用JSON,那么是否有一种简单的方法可以仅从该数据返回GBP值

public class Currency
    {
        public List<string> rates { get; set; }
    }

    public decimal convertCurrency(decimal amount, string fromCurrency, string toCurrency)
    {
        WebClient client = new WebClient();
        decimal rate = 0;
        string url = String.Format("https://openexchangerates.org/api/latest.json?app_id={0}", ConfigurationManager.AppSettings["OpenExchangeRate_AppID"]);

        using (StreamReader r = new StreamReader(client.OpenRead(url)))
        {
            string json = r.ReadToEnd();
            **List<Currency> items = JsonConvert.DeserializeObject<List<Currency>>(json);**

            foreach (var item in items[0].rates)
            {
                if (item == toCurrency.ToUpper())
                {
                    // currency matches
                    rate = Convert.ToDecimal(item);
                }
            }
        }

        return rate;
    }
得到这样的价格

var items = JsonConvert.DeserializeObject<Currency>(json);
        rate = items.rates.GBP;
var items=JsonConvert.DeserializeObject(json);
费率=项目。费率。英镑;
得到这样的价格

var items = JsonConvert.DeserializeObject<Currency>(json);
        rate = items.rates.GBP;
var items=JsonConvert.DeserializeObject(json);
费率=项目。费率。英镑;
得到这样的价格

var items = JsonConvert.DeserializeObject<Currency>(json);
        rate = items.rates.GBP;
var items=JsonConvert.DeserializeObject(json);
费率=项目。费率。英镑;
得到这样的价格

var items = JsonConvert.DeserializeObject<Currency>(json);
        rate = items.rates.GBP;
var items=JsonConvert.DeserializeObject(json);
费率=项目。费率。英镑;

为了让OP和其他读者将来受益,有一个在线实用程序可以让您粘贴JSON,它将为您生成C#类-

上面JSON的结果与feiyun的答案非常相似(功能相当):

输入JSON:

{"disclaimer": "Exchange rates are provided for informational purposes only, and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, NO guarantees are given whatsoever of accuracy, validity, availability, or fitness for any purpose - please use at your own risk. All usage is subject to your acceptance of the Terms and Conditions of Service, available at: https://openexchangerates.org/terms/",   
 "license": "Data sourced from various providers with public-facing APIs; copyright may apply; resale is prohibited; no warranties given of any kind. Bitcoin data provided by http://coindesk.com. All usage is subject to your acceptance of the License Agreement available at: https://openexchangerates.org/license/",   
 "timestamp": 1415858442,   
 "base": "USD",   
 "rates": { "AED": 3.672917,
            "AFN": 57.690401,
            "GBP": 0.634097   
 } 
}
jsontocsharp的输出:

public class Rates
{

    public double AED { get; set; }
    public double AFN { get; set; }
    public double GBP { get; set; }
}

public class RootObject
{

    public string disclaimer { get; set; }
    public string license { get; set; }
    public int timestamp { get; set; }
    public string @base { get; set; }
    public Rates rates { get; set; }
}

为了让OP和其他读者将来受益,有一个在线实用程序可以让您粘贴JSON,并为您生成C#类-

上面JSON的结果与feiyun的答案非常相似(功能相当):

输入JSON:

{"disclaimer": "Exchange rates are provided for informational purposes only, and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, NO guarantees are given whatsoever of accuracy, validity, availability, or fitness for any purpose - please use at your own risk. All usage is subject to your acceptance of the Terms and Conditions of Service, available at: https://openexchangerates.org/terms/",   
 "license": "Data sourced from various providers with public-facing APIs; copyright may apply; resale is prohibited; no warranties given of any kind. Bitcoin data provided by http://coindesk.com. All usage is subject to your acceptance of the License Agreement available at: https://openexchangerates.org/license/",   
 "timestamp": 1415858442,   
 "base": "USD",   
 "rates": { "AED": 3.672917,
            "AFN": 57.690401,
            "GBP": 0.634097   
 } 
}
jsontocsharp的输出:

public class Rates
{

    public double AED { get; set; }
    public double AFN { get; set; }
    public double GBP { get; set; }
}

public class RootObject
{

    public string disclaimer { get; set; }
    public string license { get; set; }
    public int timestamp { get; set; }
    public string @base { get; set; }
    public Rates rates { get; set; }
}

为了让OP和其他读者将来受益,有一个在线实用程序可以让您粘贴JSON,并为您生成C#类-

上面JSON的结果与feiyun的答案非常相似(功能相当):

输入JSON:

{"disclaimer": "Exchange rates are provided for informational purposes only, and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, NO guarantees are given whatsoever of accuracy, validity, availability, or fitness for any purpose - please use at your own risk. All usage is subject to your acceptance of the Terms and Conditions of Service, available at: https://openexchangerates.org/terms/",   
 "license": "Data sourced from various providers with public-facing APIs; copyright may apply; resale is prohibited; no warranties given of any kind. Bitcoin data provided by http://coindesk.com. All usage is subject to your acceptance of the License Agreement available at: https://openexchangerates.org/license/",   
 "timestamp": 1415858442,   
 "base": "USD",   
 "rates": { "AED": 3.672917,
            "AFN": 57.690401,
            "GBP": 0.634097   
 } 
}
jsontocsharp的输出:

public class Rates
{

    public double AED { get; set; }
    public double AFN { get; set; }
    public double GBP { get; set; }
}

public class RootObject
{

    public string disclaimer { get; set; }
    public string license { get; set; }
    public int timestamp { get; set; }
    public string @base { get; set; }
    public Rates rates { get; set; }
}

为了让OP和其他读者将来受益,有一个在线实用程序可以让您粘贴JSON,并为您生成C#类-

上面JSON的结果与feiyun的答案非常相似(功能相当):

输入JSON:

{"disclaimer": "Exchange rates are provided for informational purposes only, and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, NO guarantees are given whatsoever of accuracy, validity, availability, or fitness for any purpose - please use at your own risk. All usage is subject to your acceptance of the Terms and Conditions of Service, available at: https://openexchangerates.org/terms/",   
 "license": "Data sourced from various providers with public-facing APIs; copyright may apply; resale is prohibited; no warranties given of any kind. Bitcoin data provided by http://coindesk.com. All usage is subject to your acceptance of the License Agreement available at: https://openexchangerates.org/license/",   
 "timestamp": 1415858442,   
 "base": "USD",   
 "rates": { "AED": 3.672917,
            "AFN": 57.690401,
            "GBP": 0.634097   
 } 
}
jsontocsharp的输出:

public class Rates
{

    public double AED { get; set; }
    public double AFN { get; set; }
    public double GBP { get; set; }
}

public class RootObject
{

    public string disclaimer { get; set; }
    public string license { get; set; }
    public int timestamp { get; set; }
    public string @base { get; set; }
    public Rates rates { get; set; }
}


您是否收到错误?否,我在下一行插入断点,但它从未命中。您是否调用过该函数?是的,我可以在上一行看到json数据,它将数据从流转换为json字符串,并且我可以看到数据在那里并且正确无误。我对列表中的项目进行了foreach操作,但当我将其更改为项目[0]时,它停止工作,我不确定发布的
Currency
完整代码的定义是什么?它可能无法正确反序列化,因为它没有JSON中其他项的属性(“免责声明”、“许可证”等)。还请注意,您可能需要一个子类来表示“rates”,其中包含国家代码和汇率(JSON不是我经常使用的东西,所以我可能是错的)。您是否收到错误?否,我在下一行插入断点,但它从未命中。您是否调用过该函数?是的,我可以在上一行看到json数据,它将其从流转换为json字符串,并且我可以看到数据在那里并且正确无误。我对列表中的项目进行了foreach操作,但当我将其更改为项目[0]时,它停止工作,我不确定发布的
Currency
完整代码的定义是什么?它可能无法正确反序列化,因为它没有JSON中其他项的属性(“免责声明”、“许可证”等)。还请注意,您可能需要一个子类来表示“rates”,其中包含国家代码和汇率(JSON不是我经常使用的东西,所以我可能是错的)。您是否收到错误?否,我在下一行插入断点,但它从未命中。您是否调用过该函数?是的,我可以在上一行看到json数据,它将其从流转换为json字符串,并且我可以看到数据在那里并且正确无误。我对列表中的项目进行了foreach操作,但当我将其更改为项目[0]时,它停止工作,我不确定发布的
Currency
完整代码的定义是什么?它可能无法正确反序列化,因为它没有JSON中其他项的属性(“免责声明”、“许可证”等)。还请注意,您可能需要一个子类来表示“rates”,其中包含国家代码和汇率(JSON不是我经常使用的东西,所以我可能是错的)。您是否收到错误?否,我在下一行插入断点,但它从未命中。您是否调用过该函数?是的,我可以在上一行看到json数据,它将其从流转换为json字符串,并且我可以看到数据在那里并且正确无误。我对列表中的项目进行了foreach操作,但当我将其更改为项目[0]时,它停止工作,我不确定发布的
Currency
完整代码的定义是什么?它可能无法正确反序列化,因为它没有JSON中其他项的属性(“免责声明”、“许可证”等)。还要注意的是,您可能需要一个包含国家代码和汇率的“rates”子类(JSON不是我经常使用的东西,所以我可能错了)。这太棒了!我很高兴知道这件事,太棒了!我很高兴知道这件事,太棒了!我很高兴知道这件事,太棒了!我很高兴知道这件事