将Jsonstring转换为C#对象列表

我想将jsonstring转换为Object列表。 请帮帮我。 如果由NewtonJson完成,将会更有帮助。

我试过了,但是不行。 我不想要那个JSON的所有值。 只是在MatrixModel中提到的那些

这是一个对象

 public class MatrixModel { public string S1 { get; set; } public string S2 { get; set; } public string S3 { get; set; } public string S4 { get; set; } public string S5 { get; set; } public string S6 { get; set; } public string S7 { get; set; } public string S8 { get; set; } public string S9 { get; set; } public string S10 { get; set; } public int ScoreIfNoMatch { get; set; } } 

这是Jsonstring

  "[ { "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" } ]" 

您可以使用json2csharp.com将您的json转换为对象模型

  • 去json2csharp.com
  • 在Box中过去你的JSON。
  • 克隆生成。
  • 你将得到你的对象模型的C#代码
  • 反序列化var model = JsonConvert.DeserializeObject<RootObject>(json); 使用NewtonJson

在这里,它会产生这样的东西:

 public class MatrixModel { public class Option { public string text { get; set; } public string selectedMarks { get; set; } } public class Model { public List<Option> options { get; set; } public int maxOptions { get; set; } public int minOptions { get; set; } public bool isAnswerRequired { get; set; } public string selectedOption { get; set; } public string answerText { get; set; } public bool isRangeType { get; set; } public string from { get; set; } public string to { get; set; } public string mins { get; set; } public string secs { get; set; } } public class Question { public int QuestionId { get; set; } public string QuestionText { get; set; } public int TypeId { get; set; } public string TypeName { get; set; } public Model Model { get; set; } } public class RootObject { public Question Question { get; set; } public string CheckType { get; set; } public string S1 { get; set; } public string S2 { get; set; } public string S3 { get; set; } public string S4 { get; set; } public string S5 { get; set; } public string S6 { get; set; } public string S7 { get; set; } public string S8 { get; set; } public string S9 { get; set; } public string S10 { get; set; } public string ScoreIfNoMatch { get; set; } } } 

然后你可以反序列化为:

 var model = JsonConvert.DeserializeObject<List<MatrixModel.RootObject>>(json); 
 public static class Helper { public static string AsJsonList<T>(List<T> tt) { return new JavaScriptSerializer().Serialize(tt); } public static string AsJson<T>(T t) { return new JavaScriptSerializer().Serialize(t); } public static List<T> AsObjectList<T>(string tt) { return new JavaScriptSerializer().Deserialize<List<T>>(tt); } public static T AsObject<T>(string t) { return new JavaScriptSerializer().Deserialize<T>(t); } } 

尝试更改ScoreIfNoMatch的types,如下所示:

  public class MatrixModel { public string S1 { get; set; } public string S2 { get; set; } public string S3 { get; set; } public string S4 { get; set; } public string S5 { get; set; } public string S6 { get; set; } public string S7 { get; set; } public string S8 { get; set; } public string S9 { get; set; } public string S10 { get; set; } // the type should be string public string ScoreIfNoMatch { get; set; } }