Json Mapping and serializing in C# - c#

i'm trying to map a json string similar to this in c# to an object.
my purpose is to send it to my cilent side (it's a jquery application)
I was wondering whether i'm doing right ?
{"tasks":[
{
"id":-1,
"name":"Gantt editor",
"code":"",
"level":0,
"status":"STATUS_ACTIVE",
"start":1372608000000,
"duration":21,
"end":1375113599999,
"startIsMilestone":true,
"endIsMilestone":false,
"collapsed":false,
"assigs":[]
},
{
"id":"tmp_fk1372575559620",
"name":"release",
"code":"",
"level":1,
"status":"STATUS_ACTIVE",
"start":1372608000000,
"duration":1,
"end":1372694399999,
"startIsMilestone":false,
"endIsMilestone":false,
"collapsed":false,
"assigs":[]
}
],
"selectedRow":8,
"deletedTaskIds":[],
"resources":
[
{
"id":"tmp_1",
"name":"Resource 1"
}
],
"roles":[
{
"id":"tmp_1",
"name":"Project Manager"
}
],
"canWrite":true,
"canWriteOnParent":true
}
and this is how i mapped it
public class Attributes
{
public List<Task> _Task { get; set; }
public List<Resource> _Resource { get; set; }
public List<Role> _Role { get; set; }
public bool _canWrite { get; set; } //"canWrite":true,
public bool _canWriteOnParent { get; set; } //"canWriteOnParent":true,
public bool _selectedRow { get; set; } //"selectedRow":0,
public string [] _DeletedTaskIds { get; set; } //"deletedTaskIds":[],
}
public class Task
{
private string _id { get; set; }
private string _name { get; set; }
private string _code { get; set; }
private int _level { get; set; }
private string _status { get; set; }
private int _start { get; set; } //“start”:1348696800000,
private int _duration { get; set; } //“duration”:10,
private int _end { get; set; } //“end”:1349906399999,
private bool _startIsMilestone { get; set; } //“startIsMilestone”:false,
private bool _endIsMilestone { get; set; } //“endIsMilestone”:false,
public List<Assign> _assigns { get; set; } //“assigs”:[…],
private string _depends { get; set; } //“depends”:”7:3,8″,
private string _description { get; set; } //“description”:”Approval of testing”,
private int _progress { get; set; } //“progress”:20
}
public class Assign
{
private string _resourceId { get; set; } //“resourceId”:”tmp_1″,
private string _id { get; set; } //“id”:”tmp_1345560373990″,
private string _roleId { get; set; } //“roleId”:”tmp_1″,
private string _effort { get; set; } //“effort”:36000000
}
public class Resource
{
private string _id { get; set; } //“id”:”tmp_1″,
private string _name { get; set; } //“name”:”Resource 1″
}
public class Role
{
private string _id { get; set; } //“id”:”tmp_1″,
private string _name { get; set; } //“name”:”Project Manager”
}
if I'm doing it right, how can I serialize it into an object and send in order to send it to client side ?

Here is a link to a fantastic tool that maps Json to C#, here is what your mapping should look like:
public class Task
{
public object id { get; set; }
public string name { get; set; }
public string code { get; set; }
public int level { get; set; }
public string status { get; set; }
public object start { get; set; }
public int duration { get; set; }
public object end { get; set; }
public bool startIsMilestone { get; set; }
public bool endIsMilestone { get; set; }
public bool collapsed { get; set; }
public List<object> assigs { get; set; }
}
public class Resource
{
public string id { get; set; }
public string name { get; set; }
}
public class Role
{
public string id { get; set; }
public string name { get; set; }
}
public class RootObject
{
public List<Task> tasks { get; set; }
public int selectedRow { get; set; }
public List<object> deletedTaskIds { get; set; }
public List<Resource> resources { get; set; }
public List<Role> roles { get; set; }
public bool canWrite { get; set; }
public bool canWriteOnParent { get; set; }
}
There is also another slick way of mapping json to C# (I believe it comes with Visual Studio 2012 -> Web Essentials), you can go Edit-> Paste Special-> Paste JSON as Classes
EDIT
Since you've asked using Json.NET (you can get it from NuGet) you can deserialize your json like this:
RootObject deserialized = JsonConvert.DeserializeObject<RootObject>(data);

The format for classes and their properties you need to change. Here is the correct format.
Try it.
public class Task
{
public object id { get; set; }
public string name { get; set; }
public string code { get; set; }
public int level { get; set; }
public string status { get; set; }
public object start { get; set; }
public int duration { get; set; }
public object end { get; set; }
public bool startIsMilestone { get; set; }
public bool endIsMilestone { get; set; }
public bool collapsed { get; set; }
public List<object> assigs { get; set; }
}
public class Resource
{
public string id { get; set; }
public string name { get; set; }
}
public class Role
{
public string id { get; set; }
public string name { get; set; }
}
public class RootObject
{
public List<Task> tasks { get; set; }
public int selectedRow { get; set; }
public List<object> deletedTaskIds { get; set; }
public List<Resource> resources { get; set; }
public List<Role> roles { get; set; }
public bool canWrite { get; set; }
public bool canWriteOnParent { get; set; }
}

Related

How De-serialize response JSON with dynamic keys?

I know that this question is asked many times but still I'm struggling to understand this. I have below json to c# converted class.
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Accessibility
{
public bool share { get; set; }
public bool comment { get; set; }
}
public class Avatar
{
public string #base { get; set; }
}
public class Beautifulnari
{
public List<Post> posts { get; set; }
public long totalViewsCount { get; set; }
}
public class CaptionSignals
{
public bool safe { get; set; }
public Unsafe #unsafe { get; set; }
}
public class Category
{
public string id { get; set; }
public string name { get; set; }
}
public class Count
{
public int likes { get; set; }
public int views { get; set; }
}
public class Datum
{
public Haythandi haythandi { get; set; }
public Beautifulnari beautifulnari { get; set; }
}
public class HashtagDatum
{
public string _id { get; set; }
public string hashTagId { get; set; }
public string hashtagName { get; set; }
}
public class Haythandi
{
public List<Post> posts { get; set; }
public int totalViewsCount { get; set; }
}
public class Loc
{
public string type { get; set; }
public List<double> coordinates { get; set; }
}
public class MediaLocation
{
public int isHLS { get; set; }
public bool isTranscoded { get; set; }
public double duration { get; set; }
public string #base { get; set; }
public string thumbNailPath { get; set; }
public string path { get; set; }
public int mediaType { get; set; }
public string f0 { get; set; }
public string thumbnail { get; set; }
public Transcoded transcoded { get; set; }
public string compressedThumbnailPath { get; set; }
public string oPath { get; set; }
public string resizedThumbnailPath { get; set; }
public Resolution resolution { get; set; }
public Thumbnails thumbnails { get; set; }
public string webPath { get; set; }
}
public class ModerationStatus
{
public int approval { get; set; }
public int payment { get; set; }
public bool isModerated { get; set; }
public string moderatedBy { get; set; }
public string approvedBy { get; set; }
public DateTime? approvalDate { get; set; }
public DateTime? moderationDate { get; set; }
public int isAccepted { get; set; }
public string acceptedBy { get; set; }
public DateTime acceptanceDate { get; set; }
public object hashtagRejectReason { get; set; }
public int isOriginalAudio { get; set; }
public object ogAudioAcceptedBy { get; set; }
public object ogAudioAcceptanceDate { get; set; }
public object ogAudioRejectReason { get; set; }
public string acceptedHashtagId { get; set; }
}
public class ModerationV2Array
{
public string moderatorId { get; set; }
public string moderatorName { get; set; }
public object moderationSignals { get; set; }
public string response { get; set; }
}
public class OwnerData
{
public Avatar avatar { get; set; }
public string _id { get; set; }
public string name { get; set; }
public string username { get; set; }
public string status { get; set; }
public string profilePic { get; set; }
public int isProfileVerified { get; set; }
public int isFollowed { get; set; }
public int tipEnabled { get; set; }
}
public class Post
{
public string _id { get; set; }
public MediaLocation mediaLocation { get; set; }
public TempMedia tempMedia { get; set; }
public Song song { get; set; }
public Count count { get; set; }
public Accessibility accessibility { get; set; }
public OwnerData ownerData { get; set; }
public ModerationStatus moderationStatus { get; set; }
public TaggedStatus taggedStatus { get; set; }
public List<string> etags { get; set; }
public bool isDuplicate { get; set; }
public bool isProcessed { get; set; }
public string caption { get; set; }
public List<string> hashtagIds { get; set; }
public string youtubeLink { get; set; }
public bool isPrivate { get; set; }
public int reportPostsCount { get; set; }
public string audioLang { get; set; }
public object cta_text { get; set; }
public string ip { get; set; }
public string country { get; set; }
public string city { get; set; }
public string versionCode { get; set; }
public string createdBy { get; set; }
public string postType { get; set; }
public string uploadSource { get; set; }
public bool isSpam { get; set; }
public bool isPremium { get; set; }
public int metaProcessed { get; set; }
public int isShoppable { get; set; }
public int isLiked { get; set; }
public bool onlyFollowers { get; set; }
public bool isPrivateByAdmin { get; set; }
public int isPinned { get; set; }
public int isPromoted { get; set; }
public int isMiningAllowed { get; set; }
public string s3RefId { get; set; }
public string userId { get; set; }
public object duplicateOfPostId { get; set; }
public List<HashtagDatum> hashtagData { get; set; }
public List<Category> categories { get; set; }
public string gender { get; set; }
public int shareCount { get; set; }
public int likeCount { get; set; }
public int commentCount { get; set; }
public int viewsCount { get; set; }
public string status { get; set; }
public string language { get; set; }
public string created_at { get; set; }
public List<object> spamReason { get; set; }
public List<object> taggedUsers { get; set; }
public int __v { get; set; }
public Loc loc { get; set; }
public string region { get; set; }
public bool? enableV2 { get; set; }
public List<ModerationV2Array> moderationV2Array { get; set; }
public bool? ignoreV2 { get; set; }
public bool? isPremiumV2 { get; set; }
public bool? isSpamV2 { get; set; }
public List<PremiumCCSignal> premiumCCSignals { get; set; }
}
public class PremiumCCSignal
{
public string moderatorId { get; set; }
public CaptionSignals captionSignals { get; set; }
}
public class Resolution
{
public int width { get; set; }
public int height { get; set; }
}
public class Root
{
public int code { get; set; }
public List<Datum> data { get; set; }
public string message { get; set; }
public bool hasmoreData { get; set; }
public object error { get; set; }
}
public class Song
{
public string _id { get; set; }
public string title { get; set; }
public string art { get; set; }
public string author { get; set; }
public string categoryName { get; set; }
public object albumName { get; set; }
public int startTime { get; set; }
public double endTime { get; set; }
public string acrId { get; set; }
}
public class TaggedStatus
{
public bool isTagged { get; set; }
public string taggedBy { get; set; }
public DateTime taggedDate { get; set; }
}
public class TempMedia
{
public int isHls { get; set; }
}
public class Thumbnails
{
public string w50 { get; set; }
public string w150 { get; set; }
public string w300 { get; set; }
public string w700 { get; set; }
}
public class Transcoded
{
public string p1024 { get; set; }
public string p480 { get; set; }
public string p720 { get; set; }
}
public class Unsafe
{
}
In this above JSON, classes mentioned below are dynamic keys into response JSON.
Haythandi
Beautifulnari
The problem is that values Haythandi and beautifulNari as class name (if convert to c#) which are actually ids of that particular record. How do I convert these id's into c# class?, I have tried multiple approaches like using Dictionaries, JObjects, dynamic and Expando classes but not result. Any help would be much appreciated.
I think you need change one of the 'character classes' to:
public class MyClassWithPosts // was Beautifulnari
{
public List<Post> posts { get; set; }
public long totalViewsCount { get; set; }
}
After that data can become a dictionary (update: a list of dictionaries):
public class Root
{
public int code { get; set; }
public List<Dictionary<string, MyClassWithPosts>> data { get; set; }

Deserialize JSON from an API that returns the same object with some properties null

i'm using this api: https://store.steampowered.com/api/appdetails?appids=847370 that returns a json object and i've created a model to deserialize it.
But there's an difference on json, if the game has mac or linux requirements, then it returns an { } object, but if it don't have, then it return an [ ].
So my problem is, i've created a model using json2charp.com and deserialized many requests, but if it don't have mac or linux requirements, then the json throws error on deserialize.
My Model:
public class RootObject : Dictionary<string, AppRoot>
{
}
public class AppRoot
{
public bool success { get; set; }
public Data data { get; set; }
}
public class PcRequirements
{
public string minimum { get; set; }
public string recommended { get; set; }
}
public class MacRequirements
{
public string minimum { get; set; }
public string recommended { get; set; }
}
public class LinuxRequirements
{
public string minimum { get; set; }
public string recommended { get; set; }
}
public class PriceOverview
{
public string currency { get; set; }
public int initial { get; set; }
public int final { get; set; }
public int discount_percent { get; set; }
public string initial_formatted { get; set; }
public string final_formatted { get; set; }
}
public class Sub
{
public int packageid { get; set; }
public string percent_savings_text { get; set; }
public int percent_savings { get; set; }
public string option_text { get; set; }
public string option_description { get; set; }
public string can_get_free_license { get; set; }
public bool is_free_license { get; set; }
public int price_in_cents_with_discount { get; set; }
}
public class PackageGroup
{
public string name { get; set; }
public string title { get; set; }
public string description { get; set; }
public string selection_text { get; set; }
public string save_text { get; set; }
public int display_type { get; set; }
public string is_recurring_subscription { get; set; }
public List<Sub> subs { get; set; }
}
public class Platforms
{
public bool windows { get; set; }
public bool mac { get; set; }
public bool linux { get; set; }
}
public class Metacritic
{
public int score { get; set; }
public string url { get; set; }
}
public class Category
{
public int id { get; set; }
public string description { get; set; }
}
public class Genre
{
public string id { get; set; }
public string description { get; set; }
}
public class Screenshot
{
public int id { get; set; }
public string path_thumbnail { get; set; }
public string path_full { get; set; }
}
public class Webm
{
public string __invalid_name__480 { get; set; }
public string max { get; set; }
}
public class Movie
{
public int id { get; set; }
public string name { get; set; }
public string thumbnail { get; set; }
public Webm webm { get; set; }
public bool highlight { get; set; }
}
public class Recommendations
{
public int total { get; set; }
}
public class Highlighted
{
public string name { get; set; }
public string path { get; set; }
}
public class Achievements
{
public int total { get; set; }
public List<Highlighted> highlighted { get; set; }
}
public class ReleaseDate
{
public bool coming_soon { get; set; }
public string date { get; set; }
}
public class SupportInfo
{
public string url { get; set; }
public string email { get; set; }
}
public class ContentDescriptors
{
public List<int> ids { get; set; }
public string notes { get; set; }
}
public class Data
{
public string type { get; set; }
public string name { get; set; }
public int steam_appid { get; set; }
public string required_age { get; set; }
public bool is_free { get; set; }
public string controller_support { get; set; }
public string detailed_description { get; set; }
public string about_the_game { get; set; }
public string short_description { get; set; }
public string supported_languages { get; set; }
public string header_image { get; set; }
public string website { get; set; }
public PcRequirements pc_requirements { get; set; }
public MacRequirements mac_requirements { get; set; }
public LinuxRequirements linux_requirements { get; set; }
public string legal_notice { get; set; }
public List<string> developers { get; set; }
public List<string> publishers { get; set; }
public PriceOverview price_overview { get; set; }
public List<int> packages { get; set; }
public List<PackageGroup> package_groups { get; set; }
public Platforms platforms { get; set; }
public Metacritic metacritic { get; set; }
public List<Category> categories { get; set; }
public List<Genre> genres { get; set; }
public List<Screenshot> screenshots { get; set; }
public List<Movie> movies { get; set; }
public Recommendations recommendations { get; set; }
public Achievements achievements { get; set; }
public ReleaseDate release_date { get; set; }
public SupportInfo support_info { get; set; }
public string background { get; set; }
public ContentDescriptors content_descriptors { get; set; }
}
Can't include json sample here, the limit will be reached, sorry. Just open it on browser, it works.
JSON With Mac Requirements (That work with my model)
https://store.steampowered.com/api/appdetails?appids=847370
JSON Without MAC Requirements (Throws error, can't deserialize on my custom model)
https://store.steampowered.com/api/appdetails?appids=731490
Is there a way to create a unique model to support these different requests? or configure deserialize?
What you'll need in this situation, since you're dealing with a non-standard API that should be returning null rather than an empty array, is a custom JsonConverter.
I've created a proof-of-concept that should work in your situation:
public class NullValueArrayConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
{
reader.Read(); //Advances to the next character so that the ArrayClose token is ignored.
return null;
}
return serializer.Deserialize(reader, objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
}
Then you just need to decorate the properties in your model accordingly:
public class Data
{
//...
[JsonConverter(typeof(NullValueArrayConverter))]
public PcRequirements pc_requirements { get; set; }
[JsonConverter(typeof(NullValueArrayConverter))]
public MacRequirements mac_requirements { get; set; }
[JsonConverter(typeof(NullValueArrayConverter))]
public LinuxRequirements linux_requirements { get; set; }
//...
}

Error with deserializing complex Json

I deserialize complex JSON (Spotify Playlist) and get root level values but I cannot get branches values. I Google problem, try different options without success but, I assume its silly mistake or lack of knowledge so therefore just ask for help what I am missing?
My classes are:
public class Playlist
{
public string collaborative { get; set; }
public string description { get; set; }
//public <ExternalUrls> external_urls { get; set; } //object
//public List<Followers> followers { get; set; } //object
public string href { get; set; }
public string id { get; set; }
//public List<Image> images { get; set; } //array
public string name { get; set; }
}
public class Tracks
{
public string href { get; set; }
public Item items { get; set; } //object
public string limit { get; set; }
public string next { get; set; }
public string offset { get; set; }
public string previous { get; set; }
public string total { get; set; }
}
And code to deserialize looks like that:
StreamReader responseFromServer = new StreamReader(myWebResponse.GetResponseStream());
var dataResponse = responseFromServer.ReadToEnd();
responseFromServer.Close();
var elements = new JavaScriptSerializer().Deserialize<Playlist>(dataResponse);
RootBuffer.AddRow();
RootBuffer.collaborative = elements.collaborative.ToString();
foreach (Tracks trs in elements.tracks)
{
TracksBuffer.AddRow();
TracksBuffer.href = trs.href.ToString()
}
I genereated the classes using this excellent site: json2csharp.com
And used your existing deserialization code successfully with the following classes. It populated all the data including the child collections (brace yourself, it's a long one):
public class Playlist
{
public bool collaborative { get; set; }
public string description { get; set; }
public ExternalUrls external_urls { get; set; }
public Followers followers { get; set; }
public string href { get; set; }
public string id { get; set; }
public List<Image> images { get; set; }
public string name { get; set; }
public Owner owner { get; set; }
public bool #public { get; set; }
public string snapshot_id { get; set; }
public Tracks tracks { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class ExternalUrls
{
public string spotify { get; set; }
}
public class Followers
{
public object href { get; set; }
public int total { get; set; }
}
public class Image
{
public object height { get; set; }
public string url { get; set; }
public object width { get; set; }
}
public class ExternalUrls2
{
public string spotify { get; set; }
}
public class Owner
{
public ExternalUrls2 external_urls { get; set; }
public string href { get; set; }
public string id { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class ExternalUrls3
{
public string spotify { get; set; }
}
public class AddedBy
{
public ExternalUrls3 external_urls { get; set; }
public string href { get; set; }
public string id { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class ExternalUrls4
{
public string spotify { get; set; }
}
public class Image2
{
public int height { get; set; }
public string url { get; set; }
public int width { get; set; }
}
public class Album
{
public string album_type { get; set; }
public List<object> available_markets { get; set; }
public ExternalUrls4 external_urls { get; set; }
public string href { get; set; }
public string id { get; set; }
public List<Image2> images { get; set; }
public string name { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class ExternalUrls5
{
public string spotify { get; set; }
}
public class Artist
{
public ExternalUrls5 external_urls { get; set; }
public string href { get; set; }
public string id { get; set; }
public string name { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class ExternalIds
{
public string isrc { get; set; }
}
public class ExternalUrls6
{
public string spotify { get; set; }
}
public class Track
{
public Album album { get; set; }
public List<Artist> artists { get; set; }
public List<object> available_markets { get; set; }
public int disc_number { get; set; }
public int duration_ms { get; set; }
public bool #explicit { get; set; }
public ExternalIds external_ids { get; set; }
public ExternalUrls6 external_urls { get; set; }
public string href { get; set; }
public string id { get; set; }
public string name { get; set; }
public int popularity { get; set; }
public string preview_url { get; set; }
public int track_number { get; set; }
public string type { get; set; }
public string uri { get; set; }
}
public class Item
{
public string added_at { get; set; }
public AddedBy added_by { get; set; }
public bool is_local { get; set; }
public Track track { get; set; }
}
public class Tracks
{
public string href { get; set; }
public List<Item> items { get; set; }
public int limit { get; set; }
public object next { get; set; }
public int offset { get; set; }
public object previous { get; set; }
public int total { get; set; }
}
Hope this helps.

Trouble using all members in a class. Why can I only use the list?

I have the following class object structure:
public class StatusObj3
{
public int status { get; set; }
public DataObj3 data { get; set; }
}
public class DataObj3
{
public string survey_id { get; set; }
public string date_created { get; set; }
public string date_modified { get; set; }
public int custom_variable_count { get; set; }
public List<Custom_VariablesObj> custom_variables { get; set; }
public int language_id { get; set; }
public int num_responses { get; set; }
public int question_count { get; set; }
public string nickname { get; set; }
public TitleObj title { get; set; }
public List<PagesObj> pages { get; set; }
}
public class Custom_VariablesObj
{
public string variable_label { get; set; }
public string question_id { get; set; }
}
public class TitleObj
{
public bool enabled { get; set; }
public string text { get; set; }
}
public class PagesObj
{
string page_id { get; set; }
string heading { get; set; }
string sub_heading { get; set; }
public List<QuestionObj> questions { get; set; }
}
public class QuestionObj
{
public string question_id { get; set; }
public string heading { get; set; }
public string position { get; set; }
public QuestionTypeObj type { get; set; }
public List<AnswerObj> answers { get; set; }
}
public class QuestionTypeObj
{
public string family { get; set; }
public string subtype { get; set; }
}
public class AnswerObj
{
public string answer_id { get; set; }
public int position { get; set; }
public string text { get; set; }
public string type { get; set; }
public bool visible { get; set; }
public int weight { get; set; }
public bool apply_all_rows { get; set; }
public bool is_answer { get; set; }
public List<ItemsObj> items { get; set; }
}
public class ItemsObj
{
public string answer_id { get; set; }
public int position { get; set; }
public string type { get; set; }
public string text { get; set; }
}
I 've deserialized json into this object via:
var results_SurveyDetails = deserializer.Deserialize<StatusObj3>(json_returned);
I'm trying to loop thru the pages by:
foreach (var page in results_SurveyDetails.data.pages)
However, not all members of page are available to use.
Only the page.questions is there and not page_id, heading and subheading.
What am I doing wrong?
You are missing public on your other properties in class PagesObj.

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.

Categories

Resources