Deserialize a JSON string with a variable number of elements

I get a JSON response from a third-party API through Javascript, which I send AJAX to the server. Here I am trying to convert this to an object. I saw a simple example of this online using a custom class, but the problem in my case is that the number of fields can change. In one case, they can be simple: UserName: Blah, Age: Blah ... In another case, it can be: UserName: Blah, Age: Blah, Favorite game: Blah.

What is the best solution here?

Thanks for any input.

PS: I try to use this code below, but I get an error: Friends do not support array deserialization.

public class Friends { public IList<IDictionary<string,string>>data {get;set;} } protected void UpdateTrigger_Click(object sender, EventArgs e) { Friends fbFriends = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Friends>(Hidden1.Value); } 
+4
source share
1 answer

Use Json.NET - you can deserialize to a custom .NET object that provides all the properties and if (thing.Property != null) to get a specific value, or you can deserialize to dynamic .

+3
source

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


All Articles