[英]json.net c# collection serialize
I have to build a JSON data that looks like the below. 我必须构建一个如下所示的JSON数据。 I am using JSON.net in C# to build the structure.
我在C#中使用JSON.net构建结构。 { "username":"xxxx", "password":"yyyy", "inputs" : [ { "name" : "cccc" }, { "name" : "dddd" } ] }
{“ username”:“ xxxx”,“ password”:“ yyyy”,“ inputs”:[{“ name”:“ cccc”},{“ name”:“ dddd”}]}
The C# code looks like this. C#代码如下所示。
**public string MethodName(ref rdServerObjects rdObjects)
{
RootObject preJsonData = new RootObject();
List<Input> inputs = new List<Input>();
Input input = new Input();
preJsonData.username = "xxxx";
preJsonData.password = "yyyy";
input.name = "cccc";
inputs.Add(input);
input.name = "dddd";
inputs.Add(input);
string postJsonData = JsonConvert.SerializeObject(preJsonData);
return postJsonData;
}
}
public class RootObject
{
public string username { get; set; }
public string password { get; set; }
public List<Input> inputs { get; set; }
}
public class Input
{
public string name { get; set; }
}**
When my code executes, the output comes as: 当我的代码执行时,输出为:
{"username":"xxxx","password":"yyyy","inputs":null} {“用户名”:“ xxxx”,“密码”:“ yyyy”,“输入”:空}
Can someone please tell me what is wrong with my code? 有人可以告诉我我的代码有什么问题吗? All help would be greatly appreciated.
所有帮助将不胜感激。
thanks 谢谢
preJsonData.inputs = inputs
... The list you are creating in your MethodName
is not at all connected to the list inside your preJsonData
. preJsonData.inputs = inputs
...您在MethodName
中创建的列表根本未连接到preJsonData
内部的列表。
public string MethodName(ref rdServerObjects rdObjects)
{
RootObject preJsonData = new RootObject();
List<Input> inputs = new List<Input>();
Input input = new Input();
preJsonData.username = "xxxx";
preJsonData.password = "yyyy";
input.name = "cccc";
inputs.Add(input);
input = new Input(); //Add this line to keep from overwriting first
input.name = "dddd";
inputs.Add(input);
//THIS LINE
preJsonData.inputs = inputs;
string postJsonData = JsonConvert.SerializeObject(preJsonData);
//string postJsonData = new JavaScriptSerializer().Serialize(preJsonData);
return postJsonData;
}
}
You didn't set the inputs
property of preJsonData
. 您没有设置
preJsonData
的inputs
属性。
List<Input> inputs = new List<Input>();
Input input = new Input();
preJsonData.username = "xxxx";
preJsonData.password = "yyyy";
input.name = "cccc";
inputs.Add(input);
input.name = "dddd";
inputs.Add(input);
preJsonData.inputs = inputs; //<-- this bad boy
string postJsonData = JsonConvert.SerializeObject(preJsonData);
//string postJsonData = new JavaScriptSerializer().Serialize(preJsonData);
return postJsonData;
As Phiter & Ron have mentioned, you didn't set the inputs
property of preJsonData
. 正如Phiter&Ron所提到的,您没有设置
preJsonData
的inputs
属性。
One more part you are missing is, not re-initialising the input
variable before setting its name
. 您缺少的另一部分是,在设置
input
变量的name
之前不要重新初始化input
变量。 This will result in you getting “dddd” in your response. 这将导致您在响应中得到“ dddd”。
Input input = new Input();
input.name = "cccc";
inputs.Add(input);
input = new Input();
input.name = "dddd";
inputs.Add(input);
preJsonData.inputs = inputs;
This would fix your code. 这将修复您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.