0

Checking previous answers (3100), can't find an straight answer for that

The problem:

When trying to parse data form a server API, the data has many different layouts depending of the data associated with the resource requested.

Some times the answer is a collection of unstructured data for some entry points and those are the ones the APP need to parse.

The Json.NET JSON framework for .NET from Newtonsoft require a prerequisite class already defined to match "a previous know structure" for the serialized JSON to be deserialized.

Apparently is for serialize and deserialize any .NET object and work with those .NET objects, but not serialize and deserialize JSON in JSON objects, and work with those objects like with the C++ Rest SDK web::json::value class.

Some requests have structured information and was solved defining .NET objects and deserialized them, but when ask for the extra resource data the answers can be (among many others):

  [
    null
  ]

or

  [
    {
      "item": "value"
    }
  ]

or

  [
    {
        "item": "value"
    },
    {
        "item2": {}
    },
    {
        "item3": {
            "feature": "string feature",
            "feature2": {
                "object feature": "large object (replaced by string for simplicity)"
            }
        }
    },
    {
        "item4": "large object (replaced by string for simplicity too)"
    }
  ]

Can anyone know if there is a way (trick, hack) to do this or way to support generic JSON objects that can be use to serialize, deserialize and query, set, modify the JSON objects?

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • 1
    you can use Newtonsoft to parse a json string in a JObject/JArray without having a predefined C# class – Jason May 26 '17 at 02:51
  • deserializing and serializing is for converting a format to and from objects, parsing is for json, and newtonsoft can parse json and you can query the json without putting it into objects – Keith Nicholas May 26 '17 at 03:27
  • 1
    modified your question to take out the request for another library as particularly pedantic people will see that as offtopic :) – Keith Nicholas May 26 '17 at 03:30

1 Answers1

0

You can use ExpandoObject:

var converter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, converter);
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • An exception was thrown: System.InvalidCastException: Specified cast is not valid. at (wrapper castclass) System.Object:__castclass_with_cache (object,intptr,intptr) at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonConverter[] converters) – TrustworthySystems May 26 '17 at 21:27
  • What was the json input you provided into it? – CodingYoshi May 27 '17 at 00:16
  • The first one (when no extra information available): ' json = "[null]"; ' – TrustworthySystems May 27 '17 at 13:19
  • Also the other 2 examples and a long live test. – TrustworthySystems May 27 '17 at 13:53
  • It work without the _ExpandoObjectConverter_ variable: _**converter**_, just **dynamic obj = JsonConvert.DeserializeObject(json);**. This create a dynamic _Newtonsoft.Json.Linq.JContainer_ base class, or a _null_ object if a "null" JSON is pass. The array with a null element generates an array container with a null token. – TrustworthySystems May 27 '17 at 15:55