Get value from JToken that may not exist (best practices)

前端 未结 6 1619
说谎
说谎 2020-12-12 13:11

What\'s the best practice for retrieving JSON values that may not even exist in C# using Json.NET?

Right now I\'m dealing with a JSON provider that returns JSON that

相关标签:
6条回答
  • 2020-12-12 13:52

    Here is how you can check if the token exists:

    if (jobject["Result"].SelectToken("Items") != null) { ... }
    

    It checks if "Items" exists in "Result".

    This is a NOT working example that causes exception:

    if (jobject["Result"]["Items"] != null) { ... }
    
    0 讨论(0)
  • 2020-12-12 13:56

    This takes care of nulls

    var body = JObject.Parse("anyjsonString");
    
    body?.SelectToken("path-string-prop")?.ToString();
    
    body?.SelectToken("path-double-prop")?.ToObject<double>();
    
    0 讨论(0)
  • 2020-12-12 13:59

    This is pretty much what the generic method Value() is for. You get exactly the behavior you want if you combine it with nullable value types and the ?? operator:

    width = jToken.Value<double?>("width") ?? 100;
    
    0 讨论(0)
  • 2020-12-12 14:02

    I would write GetValue as below

    public static T GetValue<T>(this JToken jToken, string key, T defaultValue = default(T))
    {
        dynamic ret = jToken[key];
        if (ret == null) return defaultValue;
        if (ret is JObject) return JsonConvert.DeserializeObject<T>(ret.ToString());
        return (T)ret;
    }
    

    This way you can get the value of not only the basic types but also complex objects. Here is a sample

    public class ClassA
    {
        public int I;
        public double D;
        public ClassB ClassB;
    }
    public class ClassB
    {
        public int I;
        public string S;
    }
    
    var jt = JToken.Parse("{ I:1, D:3.5, ClassB:{I:2, S:'test'} }");
    
    int i1 = jt.GetValue<int>("I");
    double d1 = jt.GetValue<double>("D");
    ClassB b = jt.GetValue<ClassB>("ClassB");
    
    0 讨论(0)
  • 2020-12-12 14:05

    You can simply typecast, and it will do the conversion for you, e.g.

    var with = (double?) jToken[key] ?? 100;
    

    It will automatically return null if said key is not present in the object, so there's no need to test for it.

    0 讨论(0)
  • 2020-12-12 14:07

    TYPE variable = jsonbody["key"]?.Value<TYPE>() ?? DEFAULT_VALUE;

    e.g.

    bool attachMap = jsonbody["map"]?.Value<bool>() ?? false;

    0 讨论(0)
提交回复
热议问题