转换JSON字符串到C#对象列表 [英] Convert Json String to C# Object List

查看:174
本文介绍了转换JSON字符串到C#对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个JSON字符串转换为对象列表。请帮帮我。如果 NewtonJson 做会更有帮助。

我试过了,但它不工作。我不想说JSON的所有值。只是这是在MatrixModel提到

这是一个对象

 公共类MatrixModel
{
    公共字符串S1 {搞定;组; }
    公共字符串S2 {搞定;组; }
    公共字符串S3 {搞定;组; }
    公共字符串S4 {搞定;组; }
    公共字符串S5 {搞定;组; }
    公共字符串S6 {搞定;组; }
    公共字符串S7 {搞定;组; }
    公共字符串S8 {搞定;组; }
    公共字符串S9 {搞定;组; }
    公共字符串S10 {搞定;组; }
    公众诠释ScoreIfNoMatch {搞定;组; }
}

这是JSON字符串

 [
      {
        题: {
          QuestionId:49,
          QuestionText:你叫什么名字,
          TYPEID:1,
          类型名:MCQ,
          模式:{
            选择:
              {
                文:拉胡,
                selectedMarks:0
              },
              {
                文:PRATIK
                selectedMarks:9
              },
              {
                文:罗希特
                selectedMarks:0
              }
            ]
            maxOptions:10,
            minOptions:0,
            isAnswerRequired:真实,
            selectedOption:1,
            answerText:,
            isRangeType:假的,
            从:,
            至: ,
            分钟:02,
            秒:04
          }
        },
        CheckType:,
        S1:,
        S2:,
        S3:,
        S4:,
        S5:,
        S6:,
        S7:,
        S8:,
        S9:PRATIK,
        S10:,
        ScoreIfNoMatch:2
      },
      {
        题: {
          QuestionId:51,
          QuestionText:你是聪明呢?
          TYPEID:3,
          类型名:是非题,
          模式:{
            选择:
              {
                文:真,
                selectedMarks:7
              },
              {
                文:假,
                selectedMarks:0
              }
            ]
            maxOptions:10,
            minOptions:0,
            isAnswerRequired:假的,
            selectedOption:3,
            answerText:,
            isRangeType:假的,
            从:,
            至: ,
            分钟:01时,
            秒:04
          }
        },
        CheckType:,
        S1:,
        S2:,
        S3:,
        S4:,
        S5:,
        S6:,
        S7:真,
        S8:,
        S9:,
        S10:,
        ScoreIfNoMatch:2
      }
    ]


解决方案

您可以使用 json2csharp.com 转换您的JSON对象模型


  • 转至 json2csharp.com

  • 过去在框中输入您的JSON。

  • 的Clik上生成。

  • 您将得到C#code为对象模型

  • 将反序列化 VAR模型= JsonConvert.DeserializeObject< RootObject>(JSON); 使用的 NewtonJson

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

 公共类MatrixModel
{
    公共类选项
    {
        公共字符串文本{搞定;组; }
        公共字符串selectedMarks {搞定;组; }
    }    公共类模型
    {
        公开名单<期权及GT;选项​​{搞定;组; }
        公众诠释maxOptions {搞定;组; }
        公众诠释minOptions {搞定;组; }
        公共BOOL isAnswerRequired {搞定;组; }
        公共字符串selectedOption {搞定;组; }
        公共字符串answerText {搞定;组; }
        公共BOOL isRangeType {搞定;组; }
        从{获得公共字符串;组; }
        公共字符串{搞定;组; }
        公共字符串分钟{搞定;组; }
        公共字符串秒{搞定;组; }
    }    公共类问题
    {
        公众诠释QuestionId {搞定;组; }
        公共字符串QuestionText {搞定;组; }
        公众诠释TYPEID {搞定;组; }
        公共字符串类型名{获得;组; }
        公众型号Model {搞定;组; }
    }    公共类RootObject
    {
        公众质疑的问题得到{;组; }
        公共字符串CheckType {搞定;组; }
        公共字符串S1 {搞定;组; }
        公共字符串S2 {搞定;组; }
        公共字符串S3 {搞定;组; }
        公共字符串S4 {搞定;组; }
        公共字符串S5 {搞定;组; }
        公共字符串S6 {搞定;组; }
        公共字符串S7 {搞定;组; }
        公共字符串S8 {搞定;组; }
        公共字符串S9 {搞定;组; }
        公共字符串S10 {搞定;组; }
        公共字符串ScoreIfNoMatch {搞定;组; }
    }
}

然后,你可以为反序列化:

  VAR模型= JsonConvert.DeserializeObject<名单,LT; MatrixModel.RootObject>>(JSON);

I want to convert a json string to a Object list. Please help me. it would be more helpful if done by NewtonJson.

I tried, but its not working. I dont want all the values of that json. just which are mentioned in the MatrixModel

This is a Object

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; }
}

This is the Json String

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

解决方案

You can use json2csharp.com to Convert your json to object model

  • Go to json2csharp.com
  • Past your JSON in the Box.
  • Clik on Generate.
  • You will get C# Code for your object model
  • Deserialize by var model = JsonConvert.DeserializeObject<RootObject>(json); using NewtonJson

Here, It will generate something like this:

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; }
    }
}

Then you can deserialize as:

var model = JsonConvert.DeserializeObject<List<MatrixModel.RootObject>>(json);

这篇关于转换JSON字符串到C#对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆