如何创建使用C#我的JSON字符串? [英] How to create my json string by using C#?

查看:169
本文介绍了如何创建使用C#我的JSON字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的JSON格式。我想创建使用C#和json.net包以下JSON字符串

I am new to json format. I want to create a following json string by using C# and json.net package.

这是我的目标JSON格式:

This is my target Json format:

{
  "GetQuestions": 
  {
    "s1":"Q1,Q2",
    "s2":"Q1,Q3",
    "s3":"Q4,Q5"
  }
}

下面,我正在存储每个学生有时questions.But,学生的总数可以是vary.for例如它可以S1,S2仅或S1,S2,S3,S4 ....这在运行时在C#。所以计算,我想这取决于学生名单制作了JSON字符串....

Here, i am storing each students questions.But sometimes, the total number of students may be vary.for example it may be s1,s2 only or s1,s2,s3,s4.... This is calculated at runtime in C#.So, i want to create the json string depending on the student list....

请指引我走出这个问题的?

Please guide me to get out of this issue?

推荐答案

的JSON是一种奇怪的,它就像学生是GetQuestion对象的属性,它应该很容易是一个List .....

The json is kind of odd, it's like the students are properties of the "GetQuestion" object, it should be easy to be a List.....

关于库,你可以使用的。

About the libraries you could use are.

  • JavaScriptSerializer
  • NewtonSoft.Json
  • SimpleJson ...

和可能有更多的人,但是这是我用什么

And there could be many more, but that are what I've used

关于JSON我不知道现在也许像这样

About the json I don't now maybe something like this

public class GetQuestions
{
    public List<Student> Questions { get; set; }
}

public class Student
{
    public string Code { get; set; }
    public string Questions { get; set; }
}

void Main()
{
    var gq = new GetQuestions
    {
        Questions = new List<Student>
        {
            new Student {Code = "s1", Questions = "Q1,Q2"},
            new Student {Code = "s2", Questions = "Q1,Q2,Q3"},
            new Student {Code = "s3", Questions = "Q1,Q2,Q4"},
            new Student {Code = "s4", Questions = "Q1,Q2,Q5"},
        }
    };

    //Using Newtonsoft.json. Dump is an extension method of [Linqpad][4]
    JsonConvert.SerializeObject(gq).Dump();
}

Linqpad

和结果是这样的。

{
     "Questions":[
        {"Code":"s1","Questions":"Q1,Q2"},
        {"Code":"s2","Questions":"Q1,Q2,Q3"},
        {"Code":"s3","Questions":"Q1,Q2,Q4"},
        {"Code":"s4","Questions":"Q1,Q2,Q5"}
      ]
 }

是的,我知道了JSON是不同的,但要与字典中的JSON。

Yes I know the json is different, but the json that you want with dictionary.

void Main()
{
    var f = new Foo
    {
        GetQuestions = new Dictionary<string, string>
                {
                    {"s1", "Q1,Q2"},
                    {"s2", "Q1,Q2,Q3"},
                    {"s3", "Q1,Q2,Q4"},
                    {"s4", "Q1,Q2,Q4,Q6"},
                }
    };

    JsonConvert.SerializeObject(f).Dump();
}

class Foo
{
    public Dictionary<string, string> GetQuestions { get; set; }
}

和用字典是你想要的.....

And with Dictionary is as you want it.....

{
      "GetQuestions":
       {
              "s1":"Q1,Q2",
              "s2":"Q1,Q2,Q3",
              "s3":"Q1,Q2,Q4",
              "s4":"Q1,Q2,Q4,Q6"
       }
 }

这篇关于如何创建使用C#我的JSON字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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