繁体   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