0

I'm attempting to read the contents of a JSON file using Newtonsoft JSON which is a list of dictionaries, iterate through them, and create a new list of dictionaries after rooting out the one I don't want which will eventually be written back to the JSON file.

No matter what I try, I can't seem to be able to add the JSON entries within its list back to a new List. Here's the error:

Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 
The best overloaded method match for 'System.Collections.Generic.List<System.Collections.Specialized.OrderedDictionary>.Add(System.Collections.Specialized.OrderedDictionary)' 
has some invalid arguments

Here's the JSON string I've deserialized:

[
    {
        "name":"test",
        "custom":false,
        "file":"strawberry-perl-5.10.1.2portable.zip",
        "url":"http://strawberryperl/....",
        "ver":"5.10.1",
        "csum":"f86ae4b14daf0b1162d2c4c90a9d22e4c2452a98"
    }
]

And here's my code:

dynamic customPerlList = JsonParse("perls_custom");
List<dynamic> updatedList = new List<dynamic>();

foreach (var perlStruct in customPerlList)
{
    if (perlStruct.name != perlVersionToRemove)
    {
        Console.WriteLine("match");
        updatedList.Add((OrderedDictionary)perlStruct);
    }
}

I've just started developing in C#, so my attempts using examples found while searching either aren't sinking in, or I'm missing something else. Can someone please point out the err in my ways, and what the proper approach is to do what I'm attempting?

stevieb
  • 9,065
  • 3
  • 26
  • 36
  • Which JSON library are you using? – Jacob Jul 16 '16 at 13:14
  • Are you trying to create an OrderedDictionary that has a key of all the attributes and a value of their respective values in the JSON? (e.g. dict["name"] = "test", dict["custom"] = false etc...) – keyboardP Jul 16 '16 at 13:16
  • Yes, that is correct. I successfully write this structure to JSON earlier which works ok, and appears to extract ok as well. – stevieb Jul 16 '16 at 13:18
  • This does not look like newtonsoft, please try with `JsonConvert` from the same library, you can see an example [here](http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c). Seems like the data from the deserialize is not valid, so you could try not to use `dynamic` and debug to see what is actually wrong with the deserialize – meJustAndrew Jul 16 '16 at 13:18

1 Answers1

2

The most likely problem is that typeless JSON objects typically are matched with IDictionary<string, object> interfaces in .NET libraries; OrderedDictionary does not have that interface. And really, JSON objects are not considered ordered.

Perhaps you can switch to using a regularDictionary<string, object>, or writing a specific class to serialize to/from.

If you wanted to use Dictionary<string, object>, then you should consider deserializing as follows:

var list = JsonConvert.ToObject<List<Dictionary<string, object>>>(s);
Jacob
  • 77,566
  • 24
  • 149
  • 228
  • Thanks @Jacob, I'll give that a try with the default `Dictionary` type. – stevieb Jul 16 '16 at 13:31
  • Although I had to make other sweeping changes to my code, your solution led me down the right path. I had to use `JSONConvert.DeserializeObject>>(jsonString);` as apparently my version of JSON doesn't allow `JsonConvert.ToObject()`. Thanks for your help! – stevieb Jul 16 '16 at 14:52