Deserialize JSON Array with JSON.NET JArray - c#

I'm trying to deserialize my JSON Array using Newtonsoft JSON.NET nugget:
Here's the code:
private List<TemplateTypesObj> getTemplateTypes(JArray array)
{
List<TemplateTypesObj> templateTypes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TemplateTypesObj>>(array);
return templateTypes;
}
The only issue is that DeserializeObject takes String, not an JArray object. I can do array.toString() but I'm not sure if that is a proper way to do that.

That's because a JArray doesn't really need deserializing. It's not a string/binary representation of an object (which is something you'd deserialize). It's already an object which represents your JSON. You can use it like an object - iterate through it, extract individual items from it.
Check out the docs at http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jarray.htm - there are methods in there which I'm sure could be used to achieve the conversion you want.

Related

How to Convert Json to Object in C#

I want Convert Json to Object in C#. Json here is:
[{"value":"e920ce0f-e3f5-4c6f-8e3d-d2fbc51990e4"}].
How to do it using Object.
Question seems silly but it is not so stupid.
I have not simple Json, I have IEnumerable and I am getting json from usint JsonResult like this:
new JsonResult(from c in User.Claims where (c.Type.Equals("value")) select new { c.Value });
This linq code does not work on JObject.
Thanks to Tommaso Belluzzo.
Using NewtonSoft Json.NET library (https://www.newtonsoft.com/json), you can do as follows:
JObject result = JObject.Parse(jsonString);
But your Json string looks more like an array, so probably JArray.Parse is what you need to use instead. Documentation with examples here:
https://www.newtonsoft.com/json/help/html/ParseJsonArray.htm
If you want to parse the internal elements as objects, thhe accepted answer of this question should provide you enough hints:
C# Parsing JSON array of objects

How could I access to a Dictionary objects?

I have a dictionary parsed from a JSON (more information here Can't deserialize Dictionary from JSON) and finally could get the parse done. I get an object with this structure shown in the image. How can I access the values from this object curContent? The "natural" way was trying to cast curContent.soportes[0].avisos to Aviso[] but it says it cannot cast from jArray to Aviso[].
currContent.soportes[2].Value
but this will give you the json string it looks like. You'd need to deserialize that as well

Parsing JValue to either JObject or JArray

Is there a nice way to get a json value, using var myValue = json["prop"] and insert it to a common object/interface? The value could be a json {} or an array []. I know I can insert them to a JObject and JArray, but is there a common object?
I also want to know (maybe the same answer to the above), if I can parse json from string, when again, I don't know if it's an array or an object.
The JToken type is a common base type for JObject and JArray. It is what json["prop"] would return, and if you had a JToken of either type, then you could set json["prop"] = token.

Array of Objects in C# wont serialize to JSON

I am having some trouble getting an array of objects in C# to serialize to JSON.
My Code:
friendlyNotification[] notifications = ns.friendlyNotifications.ToArray();
string json = JsonConvert.SerializeObject(notifications);
return Json(json, JsonRequestBehavior.AllowGet);
The code above return the following:
[{}]
Does anyone have and idea why this isn't working? Something to do with the fact that I am trying to serialize an array?
Thanks.
As friendlyNotification is a class without public properties, and notifications contains only one element the json result is [{}].
The json [{}] has the meaning, of an array with an empty object.

Serialize an array into JSON objects using .NET

The serialization of the array returns the following JSON:
[{"Code":"AAAA","Description":"Description of AAAA"},{"Code":"BBBB","Description":"Description of BBBB"}]
My goal is to return the following JSON:
{"AAAA":{"Description":"Description of AAAA"},"BBBB":{"Description":"Description of BBBB"}}
You can achieve something simliar (not exactly the same you are expecting) if instead of serializing an array, build a temporary Dictionary and serialize it.
var dict = new Dictionary<String, YourClass>();
foreach (YourClass yourObj in listOfObjs)
{
dict[yourObj.Code] = yourObj;
}
// then serialize "dict"
You could add a rule to your JSON serializer to make it avoid serializing "code" property in YourClass, and you will end up with a JSON object exactly as you show in your example.
You'll need to either use a class that has the "AAAA" and "BBBB" properties, or you'll need to serialize a dictionary instead. As it is, you're serializing an array and getting an array.
The table on this blog post shows several search starting points.
.Net has built-in System.Web.Script.Serialization.JavaScriptSerializer, here, with examples
The .Net one doesn't specially serialize Dictionary the way you want, but some of the others do, I believe. However, I think there are reasons NOT to want a general serialization routine the way you've requested - though I can't list them certainly.

Categories