gpt4 book ai didi

c# - 如何从 JObject 中按键获取值?

转载 作者:IT老高 更新时间:2023-10-28 12:54:00 26 4
gpt4 key购买 nike

我有一个这样的 JObject:

{
"@STARTDATE": "'2016-02-17 00:00:00.000'",
"@ENDDATE": "'2016-02-18 23:59:00.000'"
}

我想从 JObject 中获取 @STARTDATE 和 @ENDDATE 值。


这是我尝试完成任务的示例代码:

JObject json = JObject.Parse("{\"@STARTDATE\": \"'2016-02-17 00:00:00.000'\",\"@ENDDATE\": \"'2016-02-18 23:59:00.000'\"}");
var key = "@STARTDATE";

var value = GetJArrayValue(json, key);

private string GetJArrayValue(JObject yourJArray, JToken key)
{
string value = "";
foreach (JToken item in yourJArray.Children())
{
var itemProperties = item.Children<JProperty>();
//If the property name is equal to key, we get the value
var myElement = itemProperties.FirstOrDefault(x => x.Name == key.ToString());
value = myElement.Value.ToString(); //It run into an exception here because myElement is null
break;
}
return value;
}

注意:以上代码无法从JObject中按键获取值。


您能帮我找到一种方法从 JObject 中按键获取值吗?

最佳答案

这应该会有所帮助 -

var json = "{'@STARTDATE': '2016-02-17 00:00:00.000',  '@ENDDATE': '2016-02-18 23:59:00.000' }";
var fdate = JObject.Parse(json)["@STARTDATE"];

关于c# - 如何从 JObject 中按键获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37479314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com