Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add jarray Object into JObject

How to add JArray into JObject? I am getting an exception when changing the 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]"  } 
like image 764
user2882431 Avatar asked Oct 28 '13 11:10

user2882431


People also ask

How do I declare JArray?

JArray addresses = new JArray(); foreach (AddressModel address in contactAddresses) { addresses. Add(JObject. Parse( @"{""street"":""" + address. Street + @"""city"":""" + address.

Can not add JValue to JObject?

A JObject cannot directly contain a JValue , nor another JObject , for that matter; it can only contain JProperties (which can, in turn, contain other JObjects , JArrays or JValues ).

What is JArray?

JArray(Object[]) Initializes a new instance of the JArray class with the specified content. JArray(JArray) Initializes a new instance of the JArray class from another JArray object.

What is JObject C#?

JObject. It represents a JSON Object. It helps to parse JSON data and apply querying (LINQ) to filter out required data. It is presented in Newtonsoft.


2 Answers

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. 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.

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.

like image 122
Brian Rogers Avatar answered Sep 30 '22 09:09

Brian Rogers


// 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"   ] } 
like image 21
BillKrat Avatar answered Sep 30 '22 08:09

BillKrat