0

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);
halfer
  • 19,824
  • 17
  • 99
  • 186
Laziale
  • 7,965
  • 46
  • 146
  • 262
  • 1
    Check if this is helpful http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net?rq=1 – Claudio Redi Apr 15 '14 at 14:57
  • are you able to change the json? – Rob Allen Apr 15 '14 at 14:58
  • The JSON you provided is not key value pairs, and therefore it won't deserialize into a Dictionary. You can either modify your JSON to be key value pairs, or you can modify which C# type you deserialize to. – mason Apr 15 '14 at 15:32

1 Answers1

0

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.

Tod
  • 2,070
  • 21
  • 27