JSON.Net复杂JSON结构构建

时间:2018-09-04 18:47:25

标签: c# json json.net

我必须构建一个看起来像这样的结构:

{
  "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);

我在json中得到的输出是: “ {\”用户名\“:\” aaa \“,\”密码\“:\” bbb \“,\”输入\“:[{\”名称\“:\”公司\“,\”值\ “:\” 05375 \“},{\”名称\“:\” GLDate \“,\”值\“:\” 09/15/2018 \“}],\” detailInputs \“:[{\” name \“:\” GridData \“,\” repeatingInputs \“:[{\” inputs \“:null}]}]}”“。

我想念什么?请帮忙。

谢谢 本

0 个答案:

没有答案