1

Is it possible to convert JSON to an anonymous object if I don't have a model?

Here's an example with a model:

var json = "{\"firstName\": \"John Doe\", \"lastName\": \"Doe\"}";
var model = new { firstName = "", lastName = "" };
var result = JsonConvert.DeserializeAnonymousType(json, model);
//result: { firstName = John Doe, lastName = Doe }

How to do the same if I don't know the data structure like in the example?

The problem is I'm actually using some library and it doesn't work if I pass a dynamic object as a parameter. The only way the method I need to work is passing an anonymous object like:

instance.SomeMethod(new { firstName = "John", lastName = "Doe" })
haldo
  • 14,512
  • 5
  • 46
  • 52
Dzhambazov
  • 490
  • 2
  • 11
  • 3
    Have you considered [`dynamic`](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic)? – Fildor Nov 14 '19 at 12:28
  • 2
    The question would be: What do you want to do next? How does your code in the next line interact with this object? Do you want to access any property? How do you know the name of that property? – Oliver Nov 14 '19 at 12:30
  • Deserializing to `dynamic` as recommended below results in a `JObject`. `ExpandoObject` would be another option that might work with your 3rd party library, see [How to deserialize using JSON.Net to an anonymous type?](https://stackoverflow.com/q/7995856/3744182). Truly creating an anonymous type object in runtime is very difficult, see [How to create LINQ Expression Tree to select an anonymous type](https://stackoverflow.com/q/606104/3744182). – dbc Nov 14 '19 at 17:46

5 Answers5

3

You can use dynamic with DeserializeObject:

var json = "{\"firstName\": \"John Doe\", \"lastName\": \"Doe\"}";      
var result = JsonConvert.DeserializeObject<dynamic>(json);
var firstName = result.firstName;
haldo
  • 14,512
  • 5
  • 46
  • 52
1

You can use dynamic type but you will need to includes a reference named Microsoft.CSharp if working in Xamarin.

Here's the example:

var json = "{\"firstName\": \"John Doe\", \"lastName\": \"Doe\"}";

dynamic x = JsonConvert.DeserializeObject(json);

Console.WriteLine(x.firstname.ToString());

//You'll need to convert to data type especially in boolean, int, float, double

And here's to convert object to json:

dynamic x = new ExpandoObject();
x.param1 = "";
x.param2 = "";

string json = JsonConvert.SerializeObject(x);
Mr Hery
  • 829
  • 1
  • 7
  • 25
1

Using dynamic:

var json = "{\"firstName\": \"John Doe\", \"lastName\": \"Doe\"}";      
dynamic result = JsonConvert.DeserializeObject(json);

Also you can check if the property exists before access to it and avoid crashes with this code:

var dictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(result .ToString());
if (dictionary.ContainsKey("firstName"))  
//do something with "firstName"

Hope it works

Víctor Beltrán
  • 593
  • 5
  • 12
1

In System.Dynamic namespace, there is a sealed class named ExpandoObject. If you are using Newtonsoft.Json library, it will do it for you. You don't need to know data structure at runtime.

var json = "{\"firstName\": \"John\", \"lastName\": \"Doe\"}";
var obj = JsonConvert.DeserializeObject<ExpandoObject>(json);
var returnData = new ReturnData(){
 Id = 1,
 Value = "Hello",
 State = true,
 Json = obj
};
return returnData;

Output:

{
  "id": 1,
  "value": "Hello",
  "state": true,
  "json": {
    "firstName": "John",
    "lastName": "Doe"
  }
}

Tested with .NET 6, works properly.

Abdullah Ilgaz
  • 719
  • 1
  • 17
  • 39
-3

Try to use GSon, you should import the dependency