C# 如何将 jarray 对象添加到 JObject 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19634153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add jarray Object into JObject
提问by user2882431
How to add JArray
into JObject
? I am getting an exception when changing the jarrayObj
into JObject
.
怎么加JArray
进去JObject
?更改jarrayObj
into时出现异常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))
I need output in this form:
我需要这种形式的输出:
{
"_delete": ["Test1","Test2","Test3"],
"birthday":"2011-05-06",
"email":"[email protected]"
}
回答by Brian Rogers
I see two problems with your code as you posted it.
在您发布代码时,我发现您的代码存在两个问题。
parameterNames
needs to be an array of strings, not just a single string with commas.- You can't add a
JArray
directly to aJObject
; you have to put it in aJProperty
and add thatto theJObject
, just like you are doing with the "birthday" and "email" properties.
parameterNames
需要是一个字符串数组,而不仅仅是一个带逗号的字符串。- 您不能将 a
JArray
直接添加到 aJObject
;你必须把它放在一个JProperty
并添加该到JObject
,就像你与“生日”和“电子邮件”特性做。
Corrected code:
更正的代码:
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 = "[email protected]";
JObject UpdateAccProfile = new JObject(
new JProperty("_delete", jarrayObj),
new JProperty("birthday", txtBday),
new JProperty("email", txtemail));
Console.WriteLine(UpdateAccProfile.ToString());
Output:
输出:
{
"_delete": [
"Test1",
"Test2",
"Test3"
],
"birthday": "2011-05-06",
"email": "[email protected]"
}
Also, for future reference, if you are getting an exception in your code, it is helpful if you say in your question exactly what the exception is, so that we don't have to guess. It makes it easier for us to help you.
此外,为了将来参考,如果您在代码中遇到异常,在您的问题中准确说出异常是什么会很有帮助,这样我们就不必猜测了。这使我们更容易为您提供帮助。
回答by user11578208
var jObject = new JObject();
jObject.Add("birthday", "2011-05-06");
jObject.Add("email", "[email protected]");
var items = new [] { "Item1", "Item2", "Item3" };
var jSonArray = JsonConvert.SerializeObject(items);
var jArray = JArray.Parse(jSonArray);
jObject.Add("_delete", jArray);
回答by BillKrat
// array of animals
var animals = new[] { "cat", "dog", "monkey" };
// our profile object
var jProfile = new JObject
{
{ "birthday", "2011-05-06" },
{ "email", "[email protected]" }
};
// Add the animals to the profile JObject
jProfile.Add("animals", JArray.FromObject(animals));
Console.Write(jProfile.ToString());
Outputs:
输出:
{
"birthday": "2011-05-06",
"email": "[email protected]",
"animals": [
"cat",
"dog",
"monkey"
]
}