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.
Related
My JSON string will be like
when I deserialize it using Newtonsoft.Json, instead of object I am getting below response
But it should be like below image
tried JsonConvert.DeserializeObject and JObject.Parse. is there any way to get in direct object structure when we deserialize it without knowing type?
Try a dynamic? e.g.
dynamic thing = JObject.Parse("{Id:123, PhoneNumber: { Primary: 12345, Secondary: 78945}");
Console.WriteLine(thing.Id);
Console.WriteLine(thing.PhoneNumber);
Console.WriteLine(thing.PhoneNumber.Primary);
I have got an issue like
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
My json file
{age : 20}
userinfo.cs
[System.Serializable]
public class UserInfo
{
public string age ;
[Newtonsoft.Json.JsonConstructor]
public UserInfo(string _age )
{
age = _age ;
}}
main.cs
public List<UserInfo>userInfoListw
userInfoListw = JsonConvert.DeserializeObject<List<UserInfo>>(Resources.Load<TextAsset>("User").ToString());
I know one of solution could be [{age : 20}],
but I do not really want to put [] into my Json file,
Please let me know another way to solve it.
I know one of solution could be [ {age : 20}], but I do not really want to put [] into my Json file, Please let me know anther way to solve it
Well, you're deserializing the JSON data into a List object. But your JSON data is not actually a List.
{age : 20}
If you deserialize it as a List, then Newtonsoft's library is going to expect a collection in the JSON (array brackets, like you pointed out). So either it's a List, or it isn't. You can either make it a collection with a single element -- which requires you to wrap the JSON in brackets, which you stated you don't want to do -- or you can change your code so it does not deserialize it into a List:
public UserInfo userInfow
userInfow = JsonConvert.DeserializeObject<UserInfo>(Resources.Load<TextAsset>("User").ToString());
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
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
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.