简体   繁体   English

如何从嵌套的JObject获取特定属性的值

[英]How to get the values of specific properties from a nested JObject

I have a JObject: 我有一个JObject:

{
  "config": {
    "DataSource": {
      "connectionString": "Localserver",

      "sqlExpression": "select id As 'key', logHeader As 'Label' from Log"
    }
  }
}

How can I get the value of connectionString and sqlExpression ? 如何获取connectionStringsqlExpression的值?

I am trying: 我在尝试:

JObject result = new JObject(
             data["DataSource"]
             .SelectMany(jt => jt["properties"]));

string connectionString = result.GetValue("connectionString").ToString();
string sqlExpression = result.GetValue("sqlExpression").ToString();

However, I'm not getting the results. 但是,我没有得到结果。 Any suggestions? 有什么建议么?

For this simple JSON structure you can use the SelectToken method navigate to each property value: 对于此简单的JSON结构,可以使用SelectToken方法导航到每个属性值:

JObject result = JObject.Parse(json);
string connectionString = (string)result.SelectToken("config.DataSource.connectionString");
string sqlExpression = (string)result.SelectToken("config.DataSource.sqlExpression");

Fiddle: https://dotnetfiddle.net/00X6kX 小提琴: https : //dotnetfiddle.net/00X6kX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM