如何将jarray对象添加到JObject中

时间:2021-08-24 09:17:03

How to add JArray into JObject? I am getting an exception when changing the jarrayObj into JObject.

如何将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))

I need output in this form:

我需要这种形式的输出:

{
    "_delete": ["Test1","Test2","Test3"],
    "birthday":"2011-05-06",          
    "email":"dude@test.com" 
}

1 个解决方案

#1


18  

I see two problems with your code as you posted it.

我发布代码时发现代码存在两个问题。

  1. parameterNames needs to be an array of strings, not just a single string with commas.
  2. parameterNames需要是一个字符串数组,而不仅仅是一个带逗号的字符串。
  3. You can't add a JArray directly to a JObject; you have to put it in a JProperty and add that to the JObject, just like you are doing with the "birthday" and "email" properties.
  4. 您不能将JArray直接添加到JObject;你必须将它放在JProperty中并将其添加到JObject,就像你正在使用“birthday”和“email”属性一样。

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 = "dude@test.com";

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": "dude@test.com"
}

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.

此外,为了将来参考,如果您在代码中收到异常,如果您在问题中准确说明异常是什么,那么我们就不必猜测了。它使我们更容易帮助您。

#1


18  

I see two problems with your code as you posted it.

我发布代码时发现代码存在两个问题。

  1. parameterNames needs to be an array of strings, not just a single string with commas.
  2. parameterNames需要是一个字符串数组,而不仅仅是一个带逗号的字符串。
  3. You can't add a JArray directly to a JObject; you have to put it in a JProperty and add that to the JObject, just like you are doing with the "birthday" and "email" properties.
  4. 您不能将JArray直接添加到JObject;你必须将它放在JProperty中并将其添加到JObject,就像你正在使用“birthday”和“email”属性一样。

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 = "dude@test.com";

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": "dude@test.com"
}

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.

此外,为了将来参考,如果您在代码中收到异常,如果您在问题中准确说明异常是什么,那么我们就不必猜测了。它使我们更容易帮助您。