1

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.

2
  • Can you create a .NET object that matches the object you have serialised and then just deserialise the json into an object of that type? Commented Jul 20, 2015 at 15:42
  • 2
    It looks like what you ought to do is deserialize to a Dictionary<string,VariationResponseModel>, then you should be able to just loop through the keys (and you can transform it into a simple list if you don't need the keys). Commented Jul 20, 2015 at 15:46

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Using ToObject() would achieve the same result if it's already a JObject.
VariationResponseModel only has 2 properties (Id and Title), but the JSON object has a lot more than that.
Well, one obvious way would be to create a class for all the other properties, or expand the VariationResponseModel class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.