我真的不知道如何問這個,但基本上,
我有一個網址http://URL.com/filename.json
,我想從中獲取資料/filename.json并“將其轉換為文本”。url 檔案包含以下內容:{"CurrentVersion": "1.0"}
我想獲取CurrentVersion并使用其值 (1.0) 定義一個字串。
using Newtonsoft.Json
//Here is what I tried
string url = "http://url";
using (webClient)
{
var jsonstring = webClient.DownloadString(url);
string versionid1 = JsonConvert.DeserializeObject < CurrentVersion > jsonstring; //I am comfused here.. It won't let me continue because CurrentVersion is not defined or something.
currentver = versionid1;
Console.Write(versionid1); // To ensure
}
uj5u.com熱心網友回復:
一種可能性是利用System.Net.WebClient下載資料:
string json;
using(var webClient = new WebClient())
{
json = webClient.DownloadString("http://URL.com/filename.json");
}
下載字串后,您可以使用Json.Net 之類的框架對其進行反序列化。因為它是一個簡單的 JSON 檔案,我們可以將其反序列化為字典。這樣我們就不必為它創建一個顯式的類:
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
現在我們可以像這樣訪問版本:
var versionString = dict["CurrentVersion"];
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/338820.html
下一篇:輸入有效會員號時不執行回傳陳述句
