Many developers encounter the need to convert JSON strings into C# objects for data manipulation and processing. This task can be made easier with the help of JSON.NET, a popular library for working with JSON data in .NET applications.
Suppose you have a JSON string containing an array of objects. The objects in this array possess specific properties that correspond to the properties of a defined C# class, known as MatrixModel. To convert this JSON string into a list of MatrixModel objects, you can follow these steps:
var matrixModelList = JsonConvert.DeserializeObject<List<MatrixModel>>(json);
Example JSON:
"[ { "Question": { "QuestionId": 49, "QuestionText": "Whats your name?", "TypeId": 1, "TypeName": "MCQ", "Model": { "options": [ { "text": "Rahul", "selectedMarks": "0" }, { "text": "Pratik", "selectedMarks": "9" }, { "text": "Rohit", "selectedMarks": "0" } ], "maxOptions": 10, "minOptions": 0, "isAnswerRequired": true, "selectedOption": "1", "answerText": "", "isRangeType": false, "from": "", "to": "", "mins": "02", "secs": "04" } }, "CheckType": "", "S1": "", "S2": "", "S3": "", "S4": "", "S5": "", "S6": "", "S7": "", "S8": "", "S9": "Pratik", "S10": "", "ScoreIfNoMatch": "2" }, { "Question": { "QuestionId": 51, "QuestionText": "Are you smart?", "TypeId": 3, "TypeName": "True-False", "Model": { "options": [ { "text": "True", "selectedMarks": "7" }, { "text": "False", "selectedMarks": "0" } ], "maxOptions": 10, "minOptions": 0, "isAnswerRequired": false, "selectedOption": "3", "answerText": "", "isRangeType": false, "from": "", "to": "", "mins": "01", "secs": "04" } }, "CheckType": "", "S1": "", "S2": "", "S3": "", "S4": "", "S5": "", "S6": "", "S7": "True", "S8": "", "S9": "", "S10": "", "ScoreIfNoMatch": "2" } ]"
Example Deserialized Object:
var model = JsonConvert.DeserializeObject<List<MatrixModel.RootObject>>(json);
Now, you can easily work with the deserialized MatrixModel list, accessing and manipulating the data as needed in your C# application.
The above is the detailed content of How to Convert a JSON String to a C# Object List Using JSON.NET?. For more information, please follow other related articles on the PHP Chinese website!