有没有办法通过路径设置属性,使用Json.NET?
JObject o = JObject.Parse(@"{
'CPU': 'Intel',
'Drivers': {
'Mouse': 'HPQ',
'VideoCard' : 'NVidia'
}
}");
//something like that
o.SetByPath("Drivers.VideoCard") = "Intel";
有可能吗?
顺便说一句,我知道我可以这样做:
o["Drivers"]["VideoCard"] = "Intel";
但这不是我想要的。
答案 0 :(得分:9)
此处可以使用JObject.SelectToken
和JToken.Replace
方法来达到基本相同的效果。
static void Main(string[] args)
{
JObject obj = JObject.Parse(@"{
'CPU': 'Intel',
'Drivers': {
'Mouse': 'HPQ',
'VideoCard' : 'NVidia'
}
}");
Console.WriteLine(obj);
JToken token = obj.SelectToken("Drivers.VideoCard");
token.Replace("Intel");
Console.WriteLine(obj);
}
输出:
{
"CPU": "Intel",
"Drivers": {
"Mouse": "HPQ",
"VideoCard": "NVidia"
}
}
{
"CPU": "Intel",
"Drivers": {
"Mouse": "HPQ",
"VideoCard": "Intel"
}
}
如果您愿意,可以使用扩展方法。
static void SetByPath(this JObject obj, string path, string value)
{
JToken token = obj.SelectToken(path);
token.Replace(value);
}
答案 1 :(得分:2)
以下方法将从类似于您在问题上提供的路径获取内部对象/值:
public static object GetObjectFromPath(dynamic obj, string path)
{
string[] segments = path.Split('.');
foreach(string segment in segments)
obj = obj[segment];
return obj;
}
你会这样称呼:
// Providing that 'o' is the JObject in your question
object result = GetObjectFromPath(o, "Drivers");
// result will be of type JObject because it is an inner json object
JObject innerObject = (JObject)result;
object value = GetObjectFromPath(o, "Drivers.VideoCard");
// You may also do GetObjectFromPath(innerObject, "VideoCard");
// value will be of type JValue because it is a json value
JValue innerValue = (JValue)result;
// You may get the .NET primite value calling the 'Value' property:
object stringValue = (string)innerValue.Value;
//You may also set the value
innerValue.Value = "Intel";