Error during deserialize JSON windows phone 8 - c#

I got a api that returns this JSON code:
[{"startDate":1409553000000,
"endDate":1409570100000,
"moduleCode":"#SPLUS0EBB2C",
"activityDescription":"User Interface Design (Tjuna)","staffMembers":[],
"locations":[{"id":"E404A902125255D3330455204193CC29","name":"HAA-H1-21","key":"14124","capacity":24,"url":null,"avoidConcurrencyLocationIds":[]}],
"studentSets":["INF3s","INF4a"],
"activityTypeName":"Other","activityTypeDescription":null,"notes":null,"highlighted":false,
"timetableKeys":["2013!studentsetgroup!9EEA55042B995043A2BC5739BF428E07"]}]
To deserialize I have this code:
var responseObject = await JsonConvert.DeserializeObjectAsync<Apicalls.Rooster>(json);
class Rooster
{
public string startDate { get; set; }
public string endDate { get; set; }
public string activityDescription { get; set; }
public locations[] locations { get; set; }
public string studentSets { get; set; }
}
public class locations
{
public string name { get; set; }
}
Everytime I try to deserialize it gives a error that it's not possible to deserialize the json code. Can anyone help me with this problem?

Rooster
public class Rooster
{
public long startDate { get; set; }
public long endDate { get; set; }
public string moduleCode { get; set; }
public string activityDescription { get; set; }
public List<object> staffMembers { get; set; }
public List<Location> locations { get; set; }
public List<string> studentSets { get; set; }
public string activityTypeName { get; set; }
public object activityTypeDescription { get; set; }
public object notes { get; set; }
public bool highlighted { get; set; }
public List<string> timetableKeys { get; set; }
}
Location
public class Location
{
public string id { get; set; }
public string name { get; set; }
public string key { get; set; }
public int capacity { get; set; }
public object url { get; set; }
public List<object> avoidConcurrencyLocationIds { get; set; }
}
Code:
var responseObject =
await JsonConvert.DeserializeObjectAsync<List<Apicalls.Rooster>>(json);
Demo

Related

Crestron Deserialize in C# and send array to Simpl+

Trying best how to take the following classes and deserialize a Json file to return each class values back to Simpl+. I am able to receive the Total value but anything in a list I am at a lost.
public class Client
{
public string clientId { get; set; }
public string locale { get; set; }
public string location { get; set; }
public string auxiliaryId { get; set; }
public string description { get; set; }
public string type { get; set; }
public string typeDescription { get; set; }
public Hardware hardware { get; set; }
public Network network { get; set; }
}
public class Hardware
{
public string type { get; set; }
public string softwareVersion { get; set; }
public string serialNumber { get; set; }
public string hardwareVersion { get; set; }
public string model { get; set; }
}
public class Network
{
public string ip { get; set; }
public string mac { get; set; }
public object homepage { get; set; }
public string dhcpSubnet { get; set; }
}
public class Result
{
public List<Client> clients { get; set; }
public string total { get; set; }
public int limit { get; set; }
public int page { get; set; }
}
public class Root
{
public string jsonrpc { get; set; }
public object id { get; set; }
public Result result { get; set; }
}
I was able to resolve this by adding the following to the foreach statement:
args.MyIndex = (ushort)(rootObject.TriplePlayResult.clients.IndexOf(item) + 1);

Got stuck at deserializing a JSON web response

I have some problems deserializing the following JSON string. On all of my other responses it works really good but not for this one. I tried so many different things and I checked the Stream class about 100 times. I also have used a generated class from json2csharp but all my deserialized Data is NULL.
My code to get the response:
public Stream getStream(string userID)
{
var request = GetRequest("streams/{channelID}", Method.GET);
request.AddUrlSegment("channelID", userID);
var response = restClient.Execute<Stream>(request);
return response.Data;
}
response.Content shows me as example something like that:
{
"stream":
{
"_id":25649270464,
"game":"test",
"broadcast_platform":"other",
"community_id":"xxxxxxxxx",
"community_ids":["xxxxxx"],
"viewers":2421,
"video_height":1080,
"average_fps":61.7876424715,
"delay":0,
"created_at":"2017-07-02T10:01:28Z",
"is_playlist":false,
"stream_type":"live",
"preview":
{
"small":"https://static-cdn.jtvnw.net/previews-ttv/live_user_lostaiming-80x45.jpg",
"medium":"https://static-cdn.jtvnw.net/previews-ttv/live_user_lostaiming-320x180.jpg",
"large":"https://static-cdn.jtvnw.net/previews-ttv/live_user_lostaiming-640x360.jpg",
"template":"https://static-cdn.jtvnw.net/previews-ttv/live_user_lostaiming-{width}x{height}.jpg"
},
"channel":
{
"mature":false,
"status":"Test"
,"broadcaster_language":"de",
"display_name":"LOSTAIMING",
"game":"test",
"language":"en",
"_id":44281267,
"name":"lostaiming",
"created_at":"2013-06-02T16:42:19.329009Z",
"updated_at":"2017-07-02T13:05:11.555285Z",
"partner":true,
"logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/lostaiming-profile_image-e9d7ea0893748d6a-300x300.png",
"video_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/cc34c6b909a435ae-channel_offline_image-1920x1080.png",
"profile_banner":"https://static-cdn.jtvnw.net/jtv_user_pictures/c187e8871c0f6a2b-profile_banner-480.png",
"profile_banner_background_color":"",
"url":"https://www.twitch.tv/lostaiming",
"views":2292173,
"followers":55672,
"broadcaster_type":"",
"description":"Blubb"
}
}
}
And my Stream class looks like this:
class Stream
{
[JsonProperty("stream")]
public SubStream stream { get; set; }
}
class SubStream
{
[JsonProperty("_id")]
public string ID { get; set; }
[JsonProperty("game")]
public string Game { get; set; }
[JsonProperty("broadcast_platform")]
public string BroadcastPlatform { get; set; }
[JsonProperty("community_id")]
public string CommunityID { get; set; }
[JsonProperty("community_ids")]
public List<object> CommunityIDS { get; set; }
[JsonProperty("viewers")]
public long Viewers { get; set; }
[JsonProperty("video_height")]
public long VideoHeigt { get; set; }
[JsonProperty("average_fps")]
public double AverageFps { get; set; }
[JsonProperty("delay")]
public long Delay { get; set; }
[JsonProperty("createt_at")]
public DateTime CreatetAt { get; set; }
[JsonProperty("is_playlist")]
public bool IsPlaylist { get; set; }
[JsonProperty("stream_type")]
public string StreamType { get; set; }
[JsonProperty("preview")]
public StreamPreview Preview { get; set; }
[JsonProperty("channel")]
public Channel channel { get; set; }
}
class StreamPreview
{
[JsonProperty("small")]
public string SmallPreview { get; set; }
[JsonProperty("medium")]
public string MediumPreview { get; set; }
[JsonProperty("large")]
public string LargPreview { get; set; }
[JsonProperty("template")]
public string TemplatePreview { get; set; }
}
public class Channel
{
[JsonProperty("mature")]
public bool Mature { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("broadcaster_language")]
public string BroadcasterLanguage { get; set; }
[JsonProperty("display_name")]
public string DisplayName { get; set; }
[JsonProperty("game")]
public string Game { get; set; }
[JsonProperty("language")]
public string Language { get; set; }
[JsonProperty("_id")]
public string ID { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("created_at")]
public DateTime CreatedAt { get; set; }
[JsonProperty("updated_at")]
public DateTime UpdatedAt { get; set; }
[JsonProperty("partner")]
public bool Partner { get; set; }
[JsonProperty("logo")]
public string Logo { get; set; }
[JsonProperty("video_banner")]
public string VideoBanner { get; set; }
[JsonProperty("profile_banner")]
public string ProfileBanner { get; set; }
[JsonProperty("profile_banner_background_color")]
public string ProfileBannerBackgroundColor { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("views")]
public long Views { get; set; }
[JsonProperty("followers")]
public long Followers { get; set; }
[JsonProperty("broadcaster_type")]
public string BroadcastType { get; set; }
[JsonProperty("stream_key")]
public string StreamKey { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
}
1) use JSON Utils (https://jsonutils.com/) to generate your C# classes (for attributes select JsonProperty)
2) Newtonsoft.Json is pretty much the standard best library to work with JSON formats in .NET, you can get it from nuget.
3) The code should look something like this, and deserialization should work without any problems:
class Program
{
private static void Main(string[] args)
{
var myObj = JsonConvert.DeserializeObject<JsonDef>(json);
Console.Read();
}
}
public class Preview
{
[JsonProperty("small")]
public string small { get; set; }
[JsonProperty("medium")]
public string medium { get; set; }
[JsonProperty("large")]
public string large { get; set; }
[JsonProperty("template")]
public string template { get; set; }
}
public class Channel
{
[JsonProperty("mature")]
public bool mature { get; set; }
[JsonProperty("status")]
public string status { get; set; }
[JsonProperty("broadcaster_language")]
public string broadcaster_language { get; set; }
[JsonProperty("display_name")]
public string display_name { get; set; }
[JsonProperty("game")]
public string game { get; set; }
[JsonProperty("language")]
public string language { get; set; }
[JsonProperty("_id")]
public int _id { get; set; }
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("created_at")]
public DateTime created_at { get; set; }
[JsonProperty("updated_at")]
public DateTime updated_at { get; set; }
[JsonProperty("partner")]
public bool partner { get; set; }
[JsonProperty("logo")]
public string logo { get; set; }
[JsonProperty("video_banner")]
public string video_banner { get; set; }
[JsonProperty("profile_banner")]
public string profile_banner { get; set; }
[JsonProperty("profile_banner_background_color")]
public string profile_banner_background_color { get; set; }
[JsonProperty("url")]
public string url { get; set; }
[JsonProperty("views")]
public int views { get; set; }
[JsonProperty("followers")]
public int followers { get; set; }
[JsonProperty("broadcaster_type")]
public string broadcaster_type { get; set; }
[JsonProperty("description")]
public string description { get; set; }
}
public class Stream
{
[JsonProperty("_id")]
public long _id { get; set; }
[JsonProperty("game")]
public string game { get; set; }
[JsonProperty("broadcast_platform")]
public string broadcast_platform { get; set; }
[JsonProperty("community_id")]
public string community_id { get; set; }
[JsonProperty("community_ids")]
public IList<string> community_ids { get; set; }
[JsonProperty("viewers")]
public int viewers { get; set; }
[JsonProperty("video_height")]
public int video_height { get; set; }
[JsonProperty("average_fps")]
public double average_fps { get; set; }
[JsonProperty("delay")]
public int delay { get; set; }
[JsonProperty("created_at")]
public DateTime created_at { get; set; }
[JsonProperty("is_playlist")]
public bool is_playlist { get; set; }
[JsonProperty("stream_type")]
public string stream_type { get; set; }
[JsonProperty("preview")]
public Preview preview { get; set; }
[JsonProperty("channel")]
public Channel channel { get; set; }
}
public class JsonDef
{
[JsonProperty("stream")]
public Stream stream { get; set; }
}
It's because of the DateTime format of the JSON. It is not in a well-formed .Net DateTime. so you can get it as a string and then convert it the way you want or change its format into a readable .Net one.

Null value in json object

I made a code for deserialize this JSON
First of all, I've created a class:
public class Self
{
public string href { get; set; }
}
public class Soccerseason
{
public string href { get; set; }
}
public class HomeTeam
{
public string href { get; set; }
}
public class AwayTeam
{
public string href { get; set; }
}
public class Links
{
public Self self { get; set; }
public Soccerseason soccerseason { get; set; }
public HomeTeam homeTeam { get; set; }
public AwayTeam awayTeam { get; set; }
}
public class Result
{
public int goalsHomeTeam { get; set; }
public int goalsAwayTeam { get; set; }
}
public class LastHomeWinHomeTeam
{
public Links _links { get; set; }
public string date { get; set; }
public object status { get; set; }
public int matchday { get; set; }
public string homeTeamName { get; set; }
public string awayTeamName { get; set; }
public Result result { get; set; }
}
public class LastWinHomeTeam
{
public Links _links { get; set; }
public string date { get; set; }
public object status { get; set; }
public int matchday { get; set; }
public string homeTeamName { get; set; }
public string awayTeamName { get; set; }
public Result result { get; set; }
}
public class LastAwayWinAwayTeam
{
public Links _links { get; set; }
public string date { get; set; }
public object status { get; set; }
public int matchday { get; set; }
public string homeTeamName { get; set; }
public string awayTeamName { get; set; }
public Result result { get; set; }
}
public class LastWinAwayTeam
{
public Links _links { get; set; }
public string date { get; set; }
public object status { get; set; }
public int matchday { get; set; }
public string homeTeamName { get; set; }
public string awayTeamName { get; set; }
public Result result { get; set; }
}
public class Head2head
{
public int count { get; set; }
public string timeFrameStart { get; set; }
public string timeFrameEnd { get; set; }
public int homeTeamWins { get; set; }
public int awayTeamWins { get; set; }
public int draws { get; set; }
public LastHomeWinHomeTeam lastHomeWinHomeTeam { get; set; }
public LastWinHomeTeam lastWinHomeTeam { get; set; }
public LastAwayWinAwayTeam lastAwayWinAwayTeam { get; set; }
public LastWinAwayTeam lastWinAwayTeam { get; set; }
public List<Fixture> fixtures { get; set; }
}
public class Fixture
{
public Links _links { get; set; }
public string date { get; set; }
public object status { get; set; }
public int matchday { get; set; }
public string homeTeamName { get; set; }
public string awayTeamName { get; set; }
public Result result { get; set; }
}
public class RootObject
{
public List<Fixture> fixture { get; set; }
public Head2head head2head { get; set; }
}
So I made a code for parsing a request, and return a responseText:
string responseText = Parser.Request(link); //Parser is the class that perform HttpRequest
so far no problem.
I've declarated the object for deserialize the responseText returned:
var obj = JsonConvert.DeserializeObject<Fixtures.RootObject>(responseText);
and next I do the foreach:
foreach (var fixture in obj.fixture)
{do stuff..}
but in obj.fixture I get null and I don't know why. Because all JSON is deserialized correcly. What am I doing wrong?
The key in the JSON is "fixtures" - it needs to match the property name of your class exactly. Change
public List<Fixture> fixture { get; set; }
to
public List<Fixture> fixtures { get; set; }
Alternatively, you can use the JsonProperty attribute:
[JsonProperty("fixtures")]
public List<Fixture> fixture { get; set; }

Could not cast or convert from System.Int64 to System.Collections.Generic.List

I am getting the following exception when I try to deserialize a JSON response from Twitter API. It sometime went through but sometime have an issue.
Below are the classes:
public static List<Tweet> Newtwt = new List<Tweet>();
public class Url2
{
public string url { get; set; }
public string expanded_url { get; set; }
public string display_url { get; set; }
public List<int> indices { get; set; }
}
public class Url
{
public List<Url2> urls { get; set; }
}
public class Description
{
public List<object> urls { get; set; }
}
public class Entities
{
public Url url { get; set; }
public Description description { get; set; }
}
public class User
{
public int id { get; set; }
public string id_str { get; set; }
public string name { get; set; }
public string screen_name { get; set; }
public string location { get; set; }
public string description { get; set; }
public string url { get; set; }
public Entities entities { get; set; }
public bool #protected { get; set; }
public int followers_count { get; set; }
public int friends_count { get; set; }
public int listed_count { get; set; }
public string created_at { get; set; }
public int favourites_count { get; set; }
public int utc_offset { get; set; }
public string time_zone { get; set; }
public bool geo_enabled { get; set; }
public bool verified { get; set; }
public int statuses_count { get; set; }
public string lang { get; set; }
public bool contributors_enabled { get; set; }
public bool is_translator { get; set; }
public string profile_background_color { get; set; }
public string profile_background_image_url { get; set; }
public string profile_background_image_url_https { get; set; }
public bool profile_background_tile { get; set; }
public string profile_image_url { get; set; }
public string profile_image_url_https { get; set; }
public string profile_link_color { get; set; }
public string profile_sidebar_border_color { get; set; }
public string profile_sidebar_fill_color { get; set; }
public string profile_text_color { get; set; }
public bool profile_use_background_image { get; set; }
public bool default_profile { get; set; }
public bool default_profile_image { get; set; }
public object following { get; set; }
public bool follow_request_sent { get; set; }
public object notifications { get; set; }
}
public class Entities2
{
public List<object> hashtags { get; set; }
public List<object> symbols { get; set; }
public List<object> urls { get; set; }
public List<object> user_mentions { get; set; }
}
public class RootObject
{
public string created_at { get; set; }
public object id { get; set; }
public string id_str { get; set; }
public string text { get; set; }
public string source { get; set; }
public bool truncated { get; set; }
public object in_reply_to_status_id { get; set; }
public object in_reply_to_status_id_str { get; set; }
public object in_reply_to_user_id { get; set; }
public object in_reply_to_user_id_str { get; set; }
public object in_reply_to_screen_name { get; set; }
public User user { get; set; }
public object geo { get; set; }
public object coordinates { get; set; }
public object place { get; set; }
public object contributors { get; set; }
public int retweet_count { get; set; }
public int favorite_count { get; set; }
public Entities2 entities { get; set; }
public bool favorited { get; set; }
public bool retweeted { get; set; }
public string lang { get; set; }
public bool? possibly_sensitive { get; set; }
}
public class Tweet
{
public string UserName { get; set; }
[JsonProperty(PropertyName = "text")]
public string Message { get; set; }
[JsonProperty(PropertyName = "id")]
public object ID { get; set; }
[JsonProperty(PropertyName = "created_at")]
public DateTime TwitTime { get; set; }
}
And I try to deserialize the response in the following way:
string str = TwitterAPI(Request.QueryString["screen_name"].ToString(), "10");
var root = JsonConvert.DeserializeObject<List<RootObject>>(str);
Where TwitterAPI() will return the response from following Twitter API:
https://api.twitter.com/1.1/statuses/user_timeline.json
Below is the detailed error:
I just found an answer.
The issue was with the limitations of Twitter API as the documentation says:
"When an application exceeds the rate limit for a given API endpoint, the Twitter API will now return an HTTP 429 error"
and I was exceeding the limit so it was giving an exception (429) Too Many Requests.
If you hit the rate limit on a given endpoint, this is the body of the HTTP 429 message that you will see:
{
"errors": [
{
"code": 88,
"message": "Rate limit exceeded"
}
]
}
So it was returning only an error message in JSON format and it was passing it to Deserialize function which in turn giving that error.
Thanks all for your suggestions.

Could not cast or convert from {null} to system.Int32 in JSON response C#

I'm getting the following exception when using this bit of code to deserialize a JSON response from CrunchBase. The weird thing is it only happens to certain pages that are being deserialized even though both the results that work fine and the ones that don't both have empty [], empty"", and null values in key:value pairs. How can I cast or correct my mistake?
Exception gets thrown here:
JsonSerializer serializer = new JsonSerializer();
RootObject ro = JsonConvert.DeserializeObject<RootObject>(response);
The inner exception is:
InnerException:
Message=Could not cast or convert from {null} to System.Int32.
Source=Newtonsoft.Json
Thanks for your eyes in advance!
Update:
as asked for the structure of the root object and the additional objects on that JSON endpoint. These were generated by http://json2csharp.com/ after putting the URL of the JSON endpoint into it.
The JSON is long so here are two example links: this one works without error http://api.crunchbase.com/v/1/company/kiip.js , while this other (and others) throws the exception http://api.crunchbase.com/v/1/company/tata-communications.js
public class Image
{
public List<List<object>> available_sizes { get; set; }
public object attribution { get; set; }
}
public class Person
{
public string first_name { get; set; }
public string last_name { get; set; }
public string permalink { get; set; }
}
public class Relationship
{
public bool is_past { get; set; }
public string title { get; set; }
public Person person { get; set; }
}
public class Provider
{
public string name { get; set; }
public string permalink { get; set; }
}
public class Providership
{
public string title { get; set; }
public bool is_past { get; set; }
public Provider provider { get; set; }
}
public class FinancialOrg
{
public string name { get; set; }
public string permalink { get; set; }
}
public class Person2
{
public string first_name { get; set; }
public string last_name { get; set; }
public string permalink { get; set; }
}
public class Investment
{
public object company { get; set; }
public FinancialOrg financial_org { get; set; }
public Person2 person { get; set; }
}
public class FundingRound
{
public string round_code { get; set; }
public string source_url { get; set; }
public string source_description { get; set; }
public double raised_amount { get; set; }
public string raised_currency_code { get; set; }
public int funded_year { get; set; }
public int funded_month { get; set; }
public int funded_day { get; set; }
public List<Investment> investments { get; set; }
}
public class Office
{
public string description { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string zip_code { get; set; }
public string city { get; set; }
public string state_code { get; set; }
public string country_code { get; set; }
public object latitude { get; set; }
public object longitude { get; set; }
}
public class VideoEmbed
{
public string embed_code { get; set; }
public string description { get; set; }
}
public class Screenshot
{
public List<List<object>> available_sizes { get; set; }
public object attribution { get; set; }
}
public class RootObject
{
public string name { get; set; }
public string permalink { get; set; }
public string crunchbase_url { get; set; }
public string homepage_url { get; set; }
public string blog_url { get; set; }
public string blog_feed_url { get; set; }
public string twitter_username { get; set; }
public string category_code { get; set; }
public int number_of_employees { get; set; }
public int founded_year { get; set; }
public int founded_month { get; set; }
public object founded_day { get; set; }
public object deadpooled_year { get; set; }
public object deadpooled_month { get; set; }
public object deadpooled_day { get; set; }
public object deadpooled_url { get; set; }
public string tag_list { get; set; }
public string alias_list { get; set; }
public string email_address { get; set; }
public string phone_number { get; set; }
public string description { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string overview { get; set; }
public Image image { get; set; }
public List<object> products { get; set; }
public List<Relationship> relationships { get; set; }
public List<object> competitions { get; set; }
public List<Providership> providerships { get; set; }
public string total_money_raised { get; set; }
public List<FundingRound> funding_rounds { get; set; }
public List<object> investments { get; set; }
public object acquisition { get; set; }
public List<object> acquisitions { get; set; }
public List<Office> offices { get; set; }
public List<object> milestones { get; set; }
public object ipo { get; set; }
public List<VideoEmbed> video_embeds { get; set; }
public List<Screenshot> screenshots { get; set; }
public List<object> external_links { get; set; }
}
Json.NET supports JSON Schema. You could create a schema with all the required properties marked and validate incoming JSON against it before deserializing. In this you can check if value is null you can make change it to some default value.
Hope this works for you.

Categories

Resources