r3plica
r3plica

Reputation: 13367

JSON.Net getting a dynamic object

I don't think the title of this post explains what the problem is, but I didn't know how to word it. Basically I have this response from an API of which I have no control over:

        "variations":{  
            "1033308042319364133":{  
                "id":"1033308042319364133",
                "order":null,
                "created_at":"2015-07-20 13:45:45",
                "updated_at":"2015-07-20 13:47:11",
                "title":"Male",
                "mod_price":"+0.00",
                "modifier":1033306667720114205,
                "product":0,
                "difference":"+£0.00"
            },
            "1033308953984892967":{  
                "id":"1033308953984892967",
                "order":null,
                "created_at":"2015-07-20 13:47:34",
                "updated_at":"2015-07-20 13:47:34",
                "title":"Female",
                "mod_price":"+0.00",
                "modifier":1033306667720114205,
                "product":0,
                "difference":"+£0.00"
            },
            "1033309404260204585":{  
                "id":"1033309404260204585",
                "order":null,
                "created_at":"2015-07-20 13:48:27",
                "updated_at":"2015-07-20 13:48:27",
                "title":"Male (Junior)",
                "mod_price":"+0.00",
                "modifier":1033306667720114205,
                "product":0,
                "difference":"+£0.00"
            },
            "1033309540147265579":{  
                "id":"1033309540147265579",
                "order":null,
                "created_at":"2015-07-20 13:48:44",
                "updated_at":"2015-07-20 13:48:44",
                "title":"Female (Junior)",
                "mod_price":"+0.00",
                "modifier":1033306667720114205,
                "product":0,
                "difference":"+£0.00"
            }
        }

in my c# code I loop through variations like this:

// Get our child variants
var variations = model["variations"];
var IsNull = IsJTokenNull(variations);
var variants = !IsNull ? new List<VariationResponseModel>() : null;

// If we have some variations
if (!IsNull)
{

    // Loop through our variations
    foreach (var variant in variations)
    {

        // Add our variant to our list
        variants.Add(CreateVariants(variant.First));
    }
}

As you can see, I am using variant.First to select the object within the property. My question is, is this the best way to do this? It seems like an awful hack.

Upvotes: 1

Views: 74

Answers (1)

SWeko
SWeko

Reputation: 30882

This looks like a .net Dictionary more than a list. If VariationResponseModel has the correct properties, you could just do:

var variants = JsonConvert.DeserializeObject<Dictionary<string, Variant>>(variations);

or using the JObject class

var variants = JObject.Parse(variations).ToObject<Dictionary<string, Variant>>();

Both approaches are equivalent, and assume that you got your input as a JSON string. If your input is already a JObject, you can just use:

var variants = variations.ToObject<Dictionary<string, Variant>>()

If you need the variants in a list/enumerable afterwards, just use variants.Values

(JsonConvert / JObject is from the Json.net deserializer)

Upvotes: 1

Related Questions