我有一个JObject:
{
"config": {
"DataSource": {
"connectionString": "Localserver",
"sqlExpression": "select id As 'key', logHeader As 'Label' from Log"
}
}
}
如何获取connectionString
和sqlExpression
的值?
我正在尝试:
JObject result = new JObject(
data["DataSource"]
.SelectMany(jt => jt["properties"]));
string connectionString = result.GetValue("connectionString").ToString();
string sqlExpression = result.GetValue("sqlExpression").ToString();
但是,我没有得到结果。 有什么建议吗?
对于这个简单的 JSON 结构,您可以使用 SelectToken
方法导航到每个属性值:
JObject result = JObject.Parse(json);
string connectionString = (string)result.SelectToken("config.DataSource.connectionString");
string sqlExpression = (string)result.SelectToken("config.DataSource.sqlExpression");
小提琴:https://dotnetfiddle.net/00X6kX