use*_*431 20 c# asp.net json.net
如何添加JArray到JObject?改变时,我得到一个异常jarrayObj进入JObject.
parameterNames = "Test1,Test2,Test3";
JArray jarrayObj = new JArray();
foreach (string parameterName in parameterNames)
{
jarrayObj.Add(parameterName);
}
JObject ObjDelParams = new JObject();
ObjDelParams["_delete"] = jarrayObj;
JObject UpdateAccProfile = new JObject(
ObjDelParams,
new JProperty("birthday", txtBday),
new JProperty("email", txtemail))
Run Code Online (Sandbox Code Playgroud)
我需要这种形式的输出:
{
"_delete": ["Test1","Test2","Test3"],
"birthday":"2011-05-06",
"email":"dude@test.com"
}
Run Code Online (Sandbox Code Playgroud)
Bri*_*ers 27
我发布代码时发现代码存在两个问题.
parameterNames 需要是一个字符串数组,而不只是一个带逗号的字符串.JArray直接添加到JObject; 你必须把它放在一个JProperty并添加该到JObject,就像你与"生日"和"电子邮件"特性做.更正代码:
string[] parameterNames = new string[] { "Test1", "Test2", "Test3" };
JArray jarrayObj = new JArray();
foreach (string parameterName in parameterNames)
{
jarrayObj.Add(parameterName);
}
string txtBday = "2011-05-06";
string txtemail = "dude@test.com";
JObject UpdateAccProfile = new JObject(
new JProperty("_delete", jarrayObj),
new JProperty("birthday", txtBday),
new JProperty("email", txtemail));
Console.WriteLine(UpdateAccProfile.ToString());
Run Code Online (Sandbox Code Playgroud)
输出:
{
"_delete": [
"Test1",
"Test2",
"Test3"
],
"birthday": "2011-05-06",
"email": "dude@test.com"
}
Run Code Online (Sandbox Code Playgroud)
此外,为了将来参考,如果您在代码中遇到异常,如果您在问题中准确说明异常是什么,那么我们就不必猜测了.它使我们更容易帮助您.
小智 9
// array of animals
var animals = new[] { "cat", "dog", "monkey" };
// our profile object
var jProfile = new JObject
{
{ "birthday", "2011-05-06" },
{ "email", "dude@test.com" }
};
// Add the animals to the profile JObject
jProfile.Add("animals", JArray.FromObject(animals));
Console.Write(jProfile.ToString());
Run Code Online (Sandbox Code Playgroud)
输出:
{
"birthday": "2011-05-06",
"email": "dude@test.com",
"animals": [
"cat",
"dog",
"monkey"
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
44849 次 |
| 最近记录: |