2

I am receiving some dynamic JSON in a windows phone app and I need to be able to completely deserialize it into objects that can be saved into isolated storage.

For this application it would be best to avoid becoming locked into a class structure because I cannot predict exactly what type of JSON I will be getting back from a server.

Here is an example of how I am using Json.NET to deserialize:

JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);

In this example the Values held in the dictionary may be Lists or Dictionaries or Strings.

The problem with this approach is that the JsonConvert.DeserializeObject(...) method seem to merely perform a shallow conversion. Nested objects are of type JArray or JObject. When I attempt to save these objects the following exception is thrown:

"Type Newtonsoft.Json.Linq.JArray with data contract name ArrayOfJTokenNewtonsoft.Json.Linq is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer."

Is there something obvious I am missing here?

On Android and iOS platforms this has been trivial to accomplish using 3rd party JSON library implementations.

Here is some sample JSON, but remember that I will not know for certain the exact structure of the JSON that I will be decoding in advance:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}
diadyne
  • 4,038
  • 36
  • 28
  • 1
    http://stackoverflow.com/questions/5546142/how-do-i-use-json-net-to-deserialize-into-nested-recursive-dictionary-and-list seems to indicate that this is not possible. I am leaving this question open with the hope that there is a way. I've worked around it for the time being by creating a class that provides access to the dictionary but stores the JSON as a string object. – diadyne May 14 '12 at 20:24
  • 1
    You can convert arbitrary JSON to a nested structure of regular .NET objects (`List`, `Dictionary`, and primitives) using Json.Net's LINQ-to-JSON API. I added [an answer](http://stackoverflow.com/a/19140420/10263) to the same question you linked showing how this can be done. – Brian Rogers Oct 02 '13 at 15:47
  • 1
    Can you provide some json string sample what are you getting from service? Thus it will be easier to answer your question. – janiss Dec 12 '13 at 16:01

0 Answers0