C# Net复杂的JSON结构构建
我必须构建一个如下所示的结构:C# Net复杂的JSON结构构建,c#,json,json.net,C#,Json,Json.net,我必须构建一个如下所示的结构: { "username":"aaa", "password":"bbb", "inputs" : [ { "name" : "Company", "value" : "05375" }, { "name" : "GLDate", "value" : "09/15/2018" } ], "detailInputs" : [ { "name" : "GridData", "repeatingInp
{
"username":"aaa",
"password":"bbb",
"inputs" : [ {
"name" : "Company",
"value" : "05375"
}, {
"name" : "GLDate",
"value" : "09/15/2018"
} ],
"detailInputs" : [ {
"name" : "GridData",
"repeatingInputs" : [ {
"inputs" : [ {
"name" : "ReceiptNbr",
"value" : "98USZJ4PR8UA"
}, {
"name" : "Payor",
"value" : "294677"
} ]
}, {
"inputs" : [ {
"name" : "ReceiptNbr",
"value" : "2XDT4I5185ZI"
}, {
"name" : "Payor",
"value" : "294677"
} ]
} ]
} ]
}
到目前为止,我已经完成了以下工作,但缺少最后一个输入结构:
public class RootObject
{
public string username { get; set; }
public string password { get; set; }
public List<Input> inputs { get; set; }
public List<DetailInput> detailInputs { get; set; }
}
public class Input
{
public string name { get; set; }
public string value { get; set; }
}
public class DetailInput
{
public string name { get; set; }
public List<RepeatingInput> repeatingInputs { get; set; }
}
public class RepeatingInput
{
public List<Input2> inputs { get; set; }
}
public class Input2
{
public string name { get; set; }
public string value { get; set; }
}
RootObject preJSONData = new RootObject();
List<Input> inputs = new List<Input>();
List<DetailInput> detailInputs = new List<DetailInput>();
List<RepeatingInput> repeatingInputs = new List<RepeatingInput>();
List<Input2> inputs2 = new List<Input2>();
preJSONData.username = "aaa";
preJSONData.password = "bbb";
/*-- PART I */
Input input = new Input();
input.name = "Company";
input.value = "05375";
inputs.Add(input);
input = new Input();
input.name = "GLDate";
input.value = "09/15/2018";
inputs.Add(input);
DetailInput dInput = new DetailInput();
dInput.name = "GridData";
dInput.repeatingInputs = repeatingInputs;
detailInputs.Add(dInput);
/*-- PART 3 */
RepeatingInput rInput = new RepeatingInput();
repeatingInputs.Add(rInput);
/*-- PART 4 */
Input2 input2 = new Input2();
input2.name = "ReceiptNbr";
input2.value = "98USZJ4PR8UA";
inputs2.Add(input2);
preJSONData.inputs = inputs;
preJSONData.detailInputs = detailInputs;
string json = JsonConvert.SerializeObject(preJSONData);
公共类根对象
{
公共字符串用户名{get;set;}
公共字符串密码{get;set;}
公共列表输入{get;set;}
公共列表输入{get;set;}
}
公共类输入
{
公共字符串名称{get;set;}
公共字符串值{get;set;}
}
公共类详细信息输入
{
公共字符串名称{get;set;}
公共列表重复输入{get;set;}
}
公共类重复输入
{
公共列表输入{get;set;}
}
公共类输入2
{
公共字符串名称{get;set;}
公共字符串值{get;set;}
}
RootObject preJSONData=新的RootObject();
列表输入=新列表();
List detailInputs=新列表();
List repeatingInputs=新列表();
列表输入2=新列表();
preJSONData.username=“aaa”;
preJSONData.password=“bbb”;
/*--第一部分*/
输入=新输入();
input.name=“公司”;
input.value=“05375”;
输入。添加(输入);
输入=新输入();
input.name=“GLDate”;
input.value=“09/15/2018”;
输入。添加(输入);
DetailInput dInput=新的DetailInput();
dInput.name=“GridData”;
dInput.repeatingInputs=repeatingInputs;
detailInputs.Add(dInput);
/*--第三部分*/
RepeatingInput rInput=新的RepeatingInput();
重复输入。添加(输入);
/*--第四部分*/
Input2 Input2=新的Input2();
input2.name=“ReceiptNbr”;
input2.value=“98USZJ4PR8UA”;
输入2.添加(输入2);
preJSONData.inputs=输入;
preJSONData.detailInputs=detailInputs;
字符串json=JsonConvert.SerializeObject(preJSONData);
我在json中得到的输出是:
“{\'username\':\'aaa\',\'password\':\'bbb\',\'inputs\':[{\'name\':\'Company\',\'value\':\'GLDate\',\'value\':\'09/15/2018\',\'detailInputs\':[{\'name\':\'GridData\',\'repeatingInputs\':[{\'inputs\'inputs\':[{\'inputs\'inputs\'null}]”
我错过了什么?请帮忙
谢谢
Ben看起来不错,而
\“
只是一种逃避”
的方法。你就快到了。缺少的是预期输出。一点格式也不会有什么坏处。快速观察:inputs2
已填充,但从未分配给任何对象。此外,您可以而且可能应该合并Input
和Input2
类。到底是什么问题?是不是整个东西都转义为JSON字符串?