簡體   English   中英

如何通過字符串獲取動態 json 值?

[英]How do I get a dynamic json value by string?

我正在做一種獲取json值的方法。 我的問題是,如果我嘗試將方法的參數實現為動態 json 值,則它不起作用。 如果我只是為該值編寫“路徑”,它確實有效。 在這段代碼中, string value= json.myDirectory.myJson; 是我的“路徑”,它也在工作。

        public static Int32 GetJson(string Directory, string json)
        {
            using (var client = new HttpClient())
            {
                var responseString = client.GetStringAsync("LinkToTheWebsite").Result.ToString();
                dynamic json = JObject.Parse(responseString);
                string value= json.myDirectory.myJson; <- this is working but if i try this, its not:
 string value= json.Directory.json
                MessageBox.Show(value);
            }
            return 1;
        }

錯誤: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The runtime binding cannot be executed for a NULL reference. 但是如果我用GetJson("myDirectory", "myJson");調用該方法GetJson("myDirectory", "myJson"); 它會導致我上面提到的錯誤,在這一行string value= json.Directory.json

字符串 jsonOffset = json[目錄][json]; 第一個 json 是我解析它的動態值,[] 中的第二個 json 是字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM