DataContractJsonSerializer returning null object - c#

I've been struggling with this problem for quite some time now, and can't solve it.
I have the following JSON string:
{"Search":[{"Title":"somestring","Year":"somestring","imdbID":"somestring"}]}, {"Title":"somestring","Year":"somestring","imdbID":"somestring"} etc
The string can repeat itself multiple times, so I want to store the values in a list. In order to do this I've created the following two classes:
The SuggestionListener class:
[DataContract]
class SuggestionLister
{
public List<MovieResults> suggestionlist {get;set;}
}
Which holds the List I want returned.
And the Movieresults class:
[DataContract]
class MovieResults
{
[DataMember]
public string Title { get; set; }
[DataMember]
public string Year { get; set; }
[DataMember]
public string imdbID { get; set; }
}
Which hold the data that needs to be stored. I tried Deserializing it with the following code:
byte[] data = Encoding.UTF8.GetBytes(resp);
MemoryStream memStream = new MemoryStream(data);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SuggestionLister));
SuggestionLister suggestionMovies = (SuggestionLister)serializer.ReadObject(memStream);
Where the 'resp' variable is the JSON string. However, when I try this code the suggestMovies object remains null. What is wrong?

Okay so there are a couple of issues:
[DataContract]
public class SuggestionLister
{
[DataMember]
public List<MovieResults> Search { get; set; }
}
You do not have DataMember attribute on your list property and it needs to match the name of the array value which is "Search".
Edit: I tested all of this using your code. Also the format of your JSON that you posted is not correct, but I am assuming that is a pasting error.

Try
[DataContract]
class SuggestionLister
{
public List<MovieResults> Search {get;set;}
}
Since your json seems to be of this format:
{
"Search": [ { "Title": ... }]
}

Related

JSON.NET: How to deserialize from specific json object to a class with a different name

This is driving me kinda nuts, because we don't have time to ask for the API team to change their response object name.
We have a json results that reads:
"seqRing": {
"admStatus": "disabled",
"status": null,
"lines": null
},
And I need that the json deserialization map it to this class:
public class SequentialRing
{
public string admStatus { get; set; }
public string status { get; set; }
public List<SeqLinesAttr> SeqLines { get; set; } = new List<SeqLinesAttr>();
}
If this were a case of a difference in the property name, I could simply use the JsonPropertyAttribute at the top of the property, but I need something similar for the class.
If there's something I could use? Please.
Thank you!
The JSON that you've shown has "seqRing" as a property of a larger JSON object. In order to deserialize that, you can create a wrapper class (or using an existing class) where you can specify the JSON property name for the class:
public class SequentialRingWrapper
{
[JsonProperty("seqRing")]
public SequentialRing SequentialRing { get; set; }
}

Json.Net Deserialize dictionary without property name

I already looked at a lot of other questions with the same problem but never found a definitive solution that actually works for me. I tried using the JsonExtensionData Attribute, that doesn't work though since I can't convert my other data class to an object and it throws the Invalid extension data attribute on 'NAMESPACE'. Member 'Sols' type must implement IDictionary<string, JToken>. error.
My current data model class looks like this
public partial class Mars
{
public Dictionary<string, Sol> Sols { get; set; }
[JsonProperty("sol_keys")]
public List<long> SolKeys { get; set; }
}
public partial class Sol
{
[JsonProperty("AT")]
public At At { get; set; }
[JsonProperty("First_UTC")]
public DateTimeOffset FirstUtc { get; set; }
[JsonProperty("Last_UTC")]
public DateTimeOffset LastUtc { get; set; }
[JsonProperty("Season")]
public string Season { get; set; }
}
public partial class At
{
[JsonProperty("av")]
public double Av { get; set; }
[JsonProperty("ct")]
public long Ct { get; set; }
[JsonProperty("mn")]
public double Mn { get; set; }
[JsonProperty("mx")]
public double Mx { get; set; }
}
The json data looks like this
{
"651":
{
"AT":
{
"av": -61.957,
"ct": 302204,
"mn": -96.733,
"mx": -15.877
},
"First_UTC": "2020-09-25T02:42:14Z",
"Last_UTC": "2020-09-26T03:21:49Z",
"Season": "summer"
},
"652": {
"AT": {
"av": -65.002,
"ct": 278608,
"mn": -96.111,
"mx": -15.653
},
"First_UTC": "2020-09-26T03:21:50Z",
"Last_UTC": "2020-09-27T04:01:24Z",
"Season": "summer"
},
"sol_keys": [
"646",
"647",
"648",
"649",
"650",
"651",
"652"
]
}
I can't really modify the json data since I get it from an api.
I basically just want to select one of the numbers and then get the Sol data of that object.
Any help would be appreciated.
The JSON doesn't fit well with the C# type system. However, you can still use Json.Net to parse it. You just need to introduce some extra steps.
First step is to parse the JSON to a JObject:
var jObject = JsonConvert.DeserializeObject<JObject>(json);
Then you can extract the sol_keys:
var solKeys = jObject.GetValue("sol_keys").ToObject<long[]>();
Now it becomes a bit tricky. If you remove the sol_keys from the JSON (in this case the parsed JSON) it has the structure of a dictionary of Sol objects that you are able to parse:
jObject.Remove("sol_keys");
var mars = jObject.ToObject<Dictionary<long, Sol>>();
Now you have both solKeys and mars parsed from the JSON. Furthermore the solKeys and the keys in the dictionary share the same type (long).

c# JSONConvert.DeserializeObject<List<RootObject>> returns nulls

I am trying to deserialize an HTTPWebRequest response (Json) to a c# object/class, but am having trouble. A collection with 10 instances of the object are returned, and all the objects are null.
Here is the json:
[
{
"id":2227,
"user_id":441,
"grades":
{"html_url":"https://...",
"current_score":91.26,
"current_grade":null,
},
"sis_account_id":"11",
"user":
{"id":441,
"name":"Nicholas Bailey",
}
},
Here are the classes:
public class Grade
{
public string html_url { get; set; }
public decimal current_score { get; set; }
public string current_grade { get; set; }
}
public class User
{
public int id { get; set; }
public string name { get; set; }
}
public class Enrollment
{
public int id { get; set; }
public int user_id { get; set; }
public Grade grades { get; set; }
public string sis_account_id { get; set; }
public User user { get; set; }
}
public class RootObject
{
public Enrollment enrollment { get; set; }
}
And here is my code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://(some uri)");
WebResponse response = request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string content = sr.ReadToEnd();
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.MetadataPropertyHandling = MetadataPropertyHandling.Ignore;
var outObject = JsonConvert.DeserializeObject<List<RootObject>>(content, settings);
I have been looking at a lot of code on google but have not found a situation that is close to mine. Also, I'm a bit green on generic collections. The code runs fine but in the debugger the "outObject" contains 10 entries, each with an "Enrollment" object that is null.
I also have tried this with json that has well over 10 entries, but it still comes up with 10. But I'm more focused on the nulls at this point (One thing at a time!). For the sake of brevity I have removed several entries in the json, but the main pattern (an enrollment object with a grades object and a user object embedded in it) remains.
Any help would be much appreciated. Thanks.
Deserialize to List<Enrollment> not List<RootObject>:
var enrollment = JsonConvert.DeserializeObject<List<Enrollment>>(content, settings);
The first JSON object in the root array has properties "id", "user_id" and so on that correspond to the members of Enrollment. Conversely there is no property "enrollment" in the JSON anywhere.
Sample fiddle.
Update
You asked, I tried this and now I'm getting an exception "Error converting value {null} to type 'System.Int32'. Path '[0].associated_user_id', line 1, position 165." associated_user_id was removed for brevity but I will add it back into my code. Any ideas?
Somewhere your data model you must have the following member (field or property):
public int associated_user_id;
Change this to a nullable:
public int? associated_user_id;

Questioning received JSON structure

I'm currently using a beta API (http://developer.riotgames.com/api/methods) which returns JSON for all the exposed methods.
I've been able to use JSON.NET to deserialize all of these return values so far. However, today I consumed one of their function which returns a JSON that is valid but is in my opinion not correct.
You're probably wondering, why don't you ask it on the beta forum? I have but I haven't received an answer so far and in general this intrigues me.
A snippet of the JSON return:
"1001": {
"name": "Boots of Speed",
"plaintext": "Slightly increases Movement Speed",
"group": "BootsNormal",
"description": "<...
}
The problem I have with this structure is that the ID is used as a "group" without an identifier. I would be able to use this decently if it had
"ItemID" : "1001"
But it doesn't have that. I don't mind manually parsing it but I'd first like to know whether or not this JSON is correct (not just valid).
Do you agree that this is not a clean way of creating a JSON block that contains a list of elements or am I missing something here? So far I haven't seen any comments on the beta forum of this API so I'm really wondering why.
Edit "valid" vs "correct/usable":
I know it's a valid JSON statement. I'm questioning the fact whether this is usable with JSON.NET.
I have the following class definition (with two subclasses):
public class JSONItem
{
[JsonProperty("tags")]
public string[] Tags { get; set; }
[JsonProperty("plaintext")]
public string Plaintext { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("into")]
public string[] Into { get; set; }
[JsonProperty("image")]
public JSONItemImage Image { get; set; }
[JsonProperty("colloq")]
public string Colloq { get; set; }
[JsonProperty("gold")]
public JSONItemGold Gold { get; set; }
}
When giving the above JSON block to to JSONConvert.DeserializeObject(json) it throws an error because "1001" is not mentioned in JSONItem.
How do you handle this so that you can use JSON.NET?
A class like this won't work because you have no names to give the properties:
public class JSONItemWrapper
{
[JsonProperty("")]
public string ID { get; set; }
[JsonProperty("")]
public JSONItem MyProperty { get; set; }
}
Edit: "consistent with other methods"
The other methods return blocks where every property is within {} and has an identifier. The most recently added function have this "primary key outside of {}" style.
It is a valid json and you can use a type like Dictionary<string, SomeObject> to deserialize your json.
string json = #"{
""1001"": {
""name"": ""Boots of Speed"",
""plaintext"": ""Slightly increases Movement Speed"",
""group"": ""BootsNormal"",
""description"": ""desc...""
}
}";
var dict = JsonConvert.DeserializeObject<Dictionary<string, MyObject>>(json);
and accesing an item later on by its key can be fast too.
public class MyObject
{
public string name { get; set; }
public string plaintext { get; set; }
public string group { get; set; }
public string description { get; set; }
}
It's annoying when APIs do things like this (using numbers as property names), but all is not lost. Simply deserialize the JSON using Json.NET and then access each of the items using the indexer operator on the parent object.
EDIT:
I almost never create DTOs when deserializing JSON. It's lots of unnecessary boilerplate in most cases. I prefer deserializing to a dynamic object, but that won't be as effective when dealing with property names that begin with digits.
Here is how I would deserialize your sample message:
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace JsonExample
{
internal class Program
{
private static void Main()
{
const string json = #"
{
'1001': {
'name': 'Boots of Speed',
'plaintext': 'Slightly increases Movement Speed',
'group': 'BootsNormal',
'description': '<...'
}
}";
var jObject = JsonConvert.DeserializeObject<JObject>(json);
var plaintext = jObject["1001"]["plaintext"].Value<string>();
Console.WriteLine(plaintext);
}
}
}
When put into http://JSONLint.com,
{
"1001": {
"name": "Boots of Speed",
"plaintext": "Slightly increases Movement Speed",
"group": "BootsNormal",
"description": "<..."
}
}
Validates as JSON.

Parsing JSON data in C#

I have a JSON data as follows
{"id": "367501354973","from": {
"name": "Bret Taylor",
"id": "220439" }
which is returned by an object(result) of IDictionary[String, Object]
In my C# code:
I have made a class for storing the JSON value which is as follows
public class SContent
{
public string id { get; set; }
public string from_name { get; set; }
public string from_id { get; set; }
}
My main C# function which stores the parses the JSON data and stores the value inside the class properties is as follows:
List<object> data = (List<object>)result["data"];
foreach (IDictionary<string, object> content in data)
{
SContent s = new SContent();
s.id = (string)content["id"];
s.from_name = (string)content["from.name"];
s.from_id = (string)content["from.id"];
}
When i execute this code, i get an exception saying System cannot find the Key "from.name" and "from.id"
When i comment the two lines (s.from_name = (string)content["from.name"];s.from_id = (string)content["from.id"];) my code runs fine.
I think i am not able to refer the nested JSON data properly.
Can anyone just validate it and please tell me how to refer nested data in JSON in C#?
Thanks
I'm not sure how you are parsing the JSON string. Are you using a class in the Framework to do the deserialization?
You could use the JavaScriptSerializer Class defined in the System.Web.Script.Serialization Namespace (you may need to add a reference to System.Web.dll)
Using that class, you would write your code like this:
public class SContent
{
public string id { get; set; }
public SFrom from { get; set; }
}
public class SFrom
{
public string name { get; set; }
public string id { get; set; }
}
Then deserialization looks like this:
var json = new JavaScriptSerializer();
var result = json.Deserialize<SContent>(/*...json text or stream...*/);
See JavaScriptSerializer on MSDN. You might also want to check out this similar question.

Categories