[英]Parse JSON string using C# script in SSIS
I am working on retrieving USD -> GBP exchange rates from CurrencyLayer using a C# script task in SSIS. 我正在使用SSIS中的C#脚本任务从CurrencyLayer检索USD - > GBP汇率。 I have used the following code:
我使用了以下代码:
string url = Dts.Variables["User::CurrencyLayerURL"].Value.ToString();
WebClient wc = new WebClient();
var jsonString = wc.DownloadString(url);
To successfully retrieve the following JSON string: 要成功检索以下JSON字符串:
{
"success":true,
"terms":"https:\/\/currencylayer.com\/terms",
"privacy":"https:\/\/currencylayer.com\/privacy",
"historical":true,
"date":"2015-11-28",
"timestamp":1448755199,
"source":"USD",
"quotes":{
"USDGBP":0.66527
}
}
However, I am not sure at this point how to retrieve just the 0.66527 value that corresponds to the "USDGBP" rate and pass it to a variable. 但是,我现在还不确定如何检索与“USDGBP”速率对应的0.66527值并将其传递给变量。 I have seen a number of suggestions to use the JSON.net library, but I am not able to add any third-party libraries to this project.
我已经看到了一些使用JSON.net库的建议,但是我无法在这个项目中添加任何第三方库。 Any help would be appreciated.
任何帮助,将不胜感激。
You can use the JsonValue
class from System.Json Namespace
. 您可以使用
System.Json Namespace
的JsonValue
类。
JsonValue value = JsonValue.Parse(jsonString);
var quote = (string)result["quotes"]["USDGBP"];
Or you could use the JavaScriptSerializer
from System.Web.Script.Serialization
或者您可以使用
System.Web.Script.Serialization
的JavaScriptSerializer
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize<ExpandoObject>(jsonString);
var quote = result.quotes.USDGBP;
Or in Json.Decode
from System.Web.Helpers
或者在
System.Web.Helpers
Json.Decode
Var result = Json.Decode(jsonString);
var quote = result.quotes.USDGBP;
Parse your json object using JSON.NET: 使用JSON.NET解析json对象:
dynamic d = JObject.Parse(jsonString );
Console.WriteLine(d.quotes.USDGBP);
You can use JavaScriptSerializer You can add Syatem.Web.Extensions namespace reference. 您可以使用JavaScriptSerializer您可以添加Syatem.Web.Extensions命名空间参考。
var serializer = new JavaScriptSerializer();
//Serialize
var serializedResult = serializer.Serialize(Object);
//Deserialize
var deserializedResult = serializer.Deserialize<OutputObjectType>(jsonString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.