c# 怎么将字符串转换成JSON

字符串 id=1&code=2&item[0][id]=13874&item[0][code]=1&item[1][id]=13875&item[1][code]=2
我想要达到的结果
{
"id":"1",
"code":"2",
"item":[
{
"id":"13874",
"code":"1"
},
{
"id":"13875",
"code":"2"
}
]
}

1、增加两个类:

 public class Root
    {
        public string id { get; set; }
        public string code { get; set; }
        public List<Child> item { get; set; }
    }

    public class Child
    {
        public string id { get; set; }
        public string code { get; set; }
    }

2、new一个类,然后填充数据:

 public MainWindow()
 {
       var root = new Root();
       root.id = "1";
       root.code = "2";
       root.item = new List<Child>();
       root.item.Add(new Child() { id = "13874",code = "1" });
       root.item.Add(new Child() { id = "13875", code = "2" });
}

3、引入Newtonsoft.Json库,然后序列号成Json字符串:

var json = JsonConvert.SerializeObject(root);

简单粗暴,直接字符串拼接。或者找对应的第三方库。