Question

I have this json value:

["67738","1","67742","1"]

I want to parse the value in C#, for each 2 values, for example, 67738, 1 is one dictionary item of two strings, then 67742 and 1 is another dictionary item with items.

I'm trying something like this:

var dict = new JavaScriptSerializer().Deserialize<Dictionary<object, object>>(modifiers);

Using that command I'm getting this error:

    Type 'System.Collections.Generic.Dictionary`2[[System.Object, mscorlib, 
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],
[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089]]' is not supported for deserialization of an array.

How can I get that working?

Edit

This is how the JSON is created, maybe I can change something on this side:

var jsonValueObj = [];
            $("#modifiersDiv :checkbox:checked").each(function() {
                jsonValueObj.push($(this).val(), $(this).attr('data-price'));
            });
           var jsonValueCol = JSON.stringify(jsonValueObj);
Was it helpful?

Solution

Try this:

var jsonValueObj = [];
        $("#modifiersDiv :checkbox:checked").each(function() {
            var v = {};
            v.value = $(this).val();
            v.price = $(this).attr('data-price');
            jsonValueObj.push(v);
        });
       var jsonValueCol = JSON.stringify(jsonValueObj);'

This should give you an array of Tuples < object, object> at the other end

EDIT:

You could also create a list of a custom class of (Value and Price) and JSON Decrypt to that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top