How to Convert JSON String to C# Object List Using Newtonsoft's JSON.NET
Scenario:
You need to convert a JSON string into a list of C# objects, specifically using the MatrixModel class, which contains various properties. The JSON string includes data for multiple instances of MatrixModel, with only a subset of the properties populated.
Conversion Approach:
Implementation:
Generate C# Model:
Deserialize JSON:
var models = JsonConvert.DeserializeObject<List<MatrixModel>>(json);
Example:
Assuming the following JSON string:
{ "questions": [ { "QuestionId": 49, "QuestionText": "What's your name?", "S9": "Pratik" }, { "QuestionId": 51, "QuestionText": "Are you smart?", "S7": "True" } ] }
Generated C# Model:
public class MatrixModel { public int QuestionId { get; set; } public string QuestionText { get; set; } public string S9 { get; set; } public string S7 { get; set; } }
public class RootObject
{
public List<MatrixModel> questions { get; set; }
}
**Deserialization:**
string json = "{...}";
var models = JsonConvert.DeserializeObject>(json);
**Note:**
The above is the detailed content of How to Deserialize a JSON String into a C# List of Objects using JSON.NET?. For more information, please follow other related articles on the PHP Chinese website!