How to recursively deserialize dynamic JSON to .NET objects using Json.NET on a Windows phone

I get dynamic JSON in a Windows Phone application, and I need to completely deserialize it to objects that can be stored in isolated storage.

For this application, it would be better to avoid locking in the class structure, because I cannot accurately predict what type of JSON I will return from the server.

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

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

In this example, the values ​​contained in the dictionary can be lists or dictionaries or strings.

The problem with this approach is that the JsonConvert.DeserializeObject (...) method seems to just do a shallow conversion. Nested objects are of type JArray or JObject. When I try to save these objects, the following exception is thrown:

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

Is there something obvious I'm missing here?

On Android and iOS, it was trivial to implement third-party JSON applications.

Here is an example of JSON, but remember that I will not know the exact structure of JSON, which I will decrypt 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" } } } } } 
+2
source share

Source: https://habr.com/ru/post/1446197/


All Articles