简体   繁体   English

如何将 jarray 对象添加到 JObject 中

[英]How to add jarray Object into JObject

How to add JArray into JObject ?如何将JArray添加到JObject I am getting an exception when changing the jarrayObj into JObject .改变时,我得到一个异常jarrayObjJObject

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" 
}

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. parameterNames需要是一个字符串数组,而不仅仅是一个带逗号的字符串。
  2. You can't add a JArray directly to a JObject ;您不能将JArray直接添加到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.你必须把它放在一个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 = "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.这使我们更容易为您提供帮助。

// 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());

Outputs:输出:

{    
  "birthday": "2011-05-06",
  "email": "dude@test.com",
  "animals": [
    "cat",
    "dog",
    "monkey"
  ]
}
var jObject = new JObject();
jObject.Add("birthday", "2011-05-06");
jObject.Add("email", "dude@test.com");
var items = new [] { "Item1", "Item2", "Item3" };
var jSonArray = JsonConvert.SerializeObject(items);
var jArray = JArray.Parse(jSonArray);
jObject.Add("_delete", jArray);

it is easy,这很容易,

JArray myarray = new JArray();
JObject myobj = new JObject();
// myobj.add(myarray); -> this is wrong. you can not add directly.

JProperty subdatalist = new JProperty("MySubData",myarray);
myobj.Add(subdata); // this is the correct way I suggest.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM