C# 如何以编程方式从动态JObject获取属性

C# 如何以编程方式从动态JObject获取属性,c#,.net,json,json.net,deserialization,C#,.net,Json,Json.net,Deserialization,我正在使用NewtonSoft JObject解析JSON字符串。 如何以编程方式从动态对象获取值? 我想简化代码,避免对每个对象重复我自己 public ExampleObject GetExampleObject(string jsonString) { ExampleObject returnObject = new ExampleObject(); dynamic dynamicResult = JObject.Parse(jsonString); if (!ReferenceEqual

我正在使用NewtonSoft JObject解析JSON字符串。 如何以编程方式从动态对象获取值? 我想简化代码,避免对每个对象重复我自己

public ExampleObject GetExampleObject(string jsonString)
{
ExampleObject returnObject = new ExampleObject();
dynamic dynamicResult = JObject.Parse(jsonString);
if (!ReferenceEquals(dynamicResult.album, null))
   {
       //code block to extract to another method if possible
       returnObject.Id = dynamicResult.album.id; 
       returnObject.Name = dynamicResult.album.name;
       returnObject.Description = dynamicResult.albumsdescription;
       //etc..
   }
else if(!ReferenceEquals(dynamicResult.photo, null))
   {
       //duplicated here
       returnObject.Id = dynamicResult.photo.id;
       returnObject.Name = dynamicResult.photo.name;
       returnObject.Description = dynamicResult.photo.description;
       //etc..
   }
else if..
//etc..

return returnObject;
}
是否有任何方法可以将“if”语句中的代码块提取到单独的方法中,例如:

private void ExampleObject GetExampleObject([string of desired type goes here? album/photo/etc])
{
  ExampleObject returnObject = new ExampleObject();
  returnObject.Id = dynamicResult.[something goes here?].id;
  returnObject.Name = dynamicResult.[something goes here?].name;
  //etc..
  return returnObject;
}
这是可能的,因为我们不能对动态对象使用反射。或者我是否正确使用了JObject


谢谢。

假设您使用的是Newtonsoft.Json.Linq.JObject,则不需要使用动态。JObject类可以使用字符串索引器,就像字典一样:

JObject myResult = GetMyResult();
returnObject.Id = myResult["string here"]["id"];

希望这有帮助

使用动态关键字,如下所示:

private static JsonSerializerSettings jsonSettings;

private static T Deserialize<T>(string jsonData)
{
   return JsonConvert.DeserializeObject<T>(jsonData, jsonSettings);
}
私有静态JsonSerializerSettings jsonSettings;
私有静态T反序列化(字符串jsonData)
{
返回JsonConvert.DeserializeObject(jsonData、jsonSettings);
}
//如果你知道什么会回来

var jresponse = Deserialize<SearchedData>(testJsonString);
var jresponse=反序列化(testJsonString);
//如果您知道返回对象类型,那么应该使用json属性对其进行签名,如

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class SearchedData
{
    [JsonProperty(PropertyName = "Currency")]
    public string Currency { get; set; }
    [JsonProperty(PropertyName = "Routes")]
    public List<List<Route>> Routes { get; set; }
}
[JsonObject(MemberSerialization=MemberSerialization.OptIn)]
公共类搜索数据
{
[JsonProperty(PropertyName=“Currency”)]
公共字符串货币{get;set;}
[JsonProperty(PropertyName=“Routes”)]
公共列表路由{get;set;}
}
//如果不知道返回类型,请使用dynamic作为泛型类型

var jresponse = Deserialize<dynamic>(testJsonString);
var jresponse=反序列化(testJsonString);

另一种方法是使用
SelectToken
(假设您使用的是
Newtonsoft.Json
):


完整文档:

jsonString是您控制的字符串吗?或者你是从另一方收到这封信,你需要和它交流吗?@MichaelD,这是从另一方收到的。我只是在接收和解析。更多答案请参见
[“string here”]
中的内容?“id”不是只返回对象中id的值吗?不区分大小写吗?@joelforsyth,是的,您可以这样做,具体取决于对象结构,例如
returnObject.id=(int)myResult[“id”]
@mardok否,不幸的是,它区分大小写,因此它可能会工作,也可能不会工作,这取决于用于序列化对象的JsonSerializer设置。什么动态关键字,您甚至没有在响应中写入动态?Joedotn我没有更正我的答案,如果您遇到任何问题,请再次写入
JObject json = GetResponse();
var name = json.SelectToken("items[0].name");