2014-03-05 55 views
20

我想將json字符串轉換爲Object列表。請幫幫我。如果通過NewtonJson完成,它會更有幫助。將Json字符串轉換爲C#對象列表

我試過了,但沒有工作。我不想要那個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; } 
} 

這是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" 
     } 
    ]" 
+1

你怎麼反序列化你的字符串到對象? – SpiderCode

+0

[將JSON字符串轉換爲C#對象]可能的重複(http://stackoverflow.com/questions/4611031/convert-json-string-to-c-sharp-object) – Liam

回答

45

您可以使用json2csharp.com將您的json轉換爲對象模型

  • 轉到json2csharp.com
  • 過去在框中輸入您的JSON。
  • Clik on Generate。
  • 你會得到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); 
+0

awesom網站! – jkl

+0

簡直太棒了。感謝您對網站的幫助 – Junaid

+0

+1,非常有幫助!有一件事,我得到一個錯誤,我不知道問題出在哪裏:'不能反序列化當前的JSON對象(例如{「name」:「value」})到類型System.Collections.Generic.List 1 [Library.BL.Unidade + RootObject],因爲該類型需要一個JSON數組(例如[1,2,3])才能正確地反序列化。 要修復這個錯誤,要麼將JSON更改爲JSON數組(例如[1,2,3]),要麼更改反序列化的類型,以便它是一個正常的.NET類型(例如不是像integer這樣的基本類型。[CODE] (http://pastebin.com/WDAXYxkE) – Terkhos

0

嘗試改變ScoreIfNoMatch的類型,就像這樣:

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; } 
     } 
5
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); 
    } 
} 
+0

這是金人,非常感謝。 – Kadaj