【问题标题】:How to get property from dynamic JObject programmatically如何以编程方式从动态 JObject 获取属性
【发布时间】:2013-04-12 10:37:05
【问题描述】:

我正在使用 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?

谢谢。

【问题讨论】:

标签: c# .net json json.net deserialization


【解决方案1】:

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

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

希望这会有所帮助!

【讨论】:

  • ["string here"] 中有什么内容? “id”不会只返回对象中 ID 的值吗?
  • 不区分大小写吗?
  • @joelforsyth,是的,您可能会这样做,具体取决于对象结构,例如returnObject.Id = (int)myResult["id"];
  • @mardok 不,不幸的是它区分大小写,因此它可能会或可能不会工作,具体取决于用于序列化对象的 JsonSerializer 设置。
【解决方案2】:

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

JObject json = GetResponse();
var name = json.SelectToken("items[0].name");

完整文档:https://www.newtonsoft.com/json/help/html/SelectToken.htm

【讨论】:

    【解决方案3】:

    使用如下动态关键字:

    private static JsonSerializerSettings jsonSettings;
    
    private static T Deserialize<T>(string jsonData)
    {
       return JsonConvert.DeserializeObject<T>(jsonData, jsonSettings);
    }
    

    //如果你知道会返回什么

    var jresponse = Deserialize<SearchedData>(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; }
    }
    

    //如果你不知道返回类型使用动态作为泛型类型

    var jresponse = Deserialize<dynamic>(testJsonString);
    

    【讨论】:

    • 什么动态关键字,你的回复中甚至没有写动态?
    • joedotnot 我已经纠正了我的答案用法,如果您遇到任何问题,请再写一次
    猜你喜欢
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多