Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to deserialize a json data from wordpress url but didn t work.
I saw many tutorials but no one worked on mine !

This is my URL : https://mahdii.000webhostapp.com/?wpapi=get_posts&dev=1[^]

I am getting Null in var serialized_post.

Please I need Help

What I have tried:

I have tried to deserialize using this code :

string json = new WebClient().DownloadString("https://mahdii.000webhostapp.com/?wpapi=get_posts&dev=1");
Console.WriteLine(json);
var deserialize_post = JsonConvert.DeserializeObject<List<mycontainer>>(json);

My Class:
namespace App8
{
    public class Author
    {
        public string id { get; set; }
        public string slug { get; set; }
        public string name { get; set; }
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string nickname { get; set; }
        public string url { get; set; }
        public string description { get; set; }
        public string gravatar { get; set; }
    }

    public class Post
    {
        public string id { get; set; }
        public string type { get; set; }
        public string slug { get; set; }
        public string url { get; set; }
        public string status { get; set; }
        public string title { get; set; }
        public string title_plain { get; set; }
        public string date { get; set; }
        public string modified { get; set; }
        public string excerpt { get; set; }
        public string parent { get; set; }
        public List<object> category { get; set; }
        public List<object> tag { get; set; }
        public List<Author> author { get; set; }
        public string comment_count { get; set; }
        public string comment_status { get; set; }
    }

    public class MyContainer
    {
        
        public List<Post> posts { get; set; }
    }
}
Posted
Updated 12-Feb-17 8:42am
v5

1 solution

One single reason: Because you are not doing it correctly. You are deserializing it to a "list of list of posts". And your JSON is a "document containing posts". I could write something as simple as,
C#
// Skip compile time type checking, first. 
dynamic deserialize_post = JsonConvert.DeserializeObject(json);

if(deserialize_post.posts != null) {
   // check if posts exist
   List<Post> posts = deserialize_post.posts;

   // Do further processing.
}

// Do the same for author etc.

Now, the problem mainly is that when the data comes from an API, you either need to map it back to the "exact" same copy of the object, or you cannot map it anywhere. You may have also seen that I used, "posts", instead of "Posts". That is because, the case also matters a lot in this case. In the case, where you are downloading the data from an API, it is recommended to always use dynamic as you may not know what is being shipped and what is not and you then check it on the runtime; dynamic skips compile time type checking, but to make sure data exists, we use if...else branching to load the data, or show an error message if the data does not exist.

For more on this, please read my article, From zero to hero in JSON with C#[^] and specially an answer at the bottom of the article that talks specifically about this problem.
 
Share this answer
 
Comments
Karthik_Mahalingam 12-Feb-17 15:21pm    
5
Afzaal Ahmad Zeeshan 12-Feb-17 15:45pm    
Thank you, Karthik.
Member 12468321 12-Feb-17 15:52pm    
Thanks a lot Afzaal Ahamad Zeeshan, i will see the article.
Thanks for your explanation now i understood.
I will try it tomorrow and post it
Afzaal Ahmad Zeeshan 12-Feb-17 16:02pm    
You're welcome.
Member 12468321 13-Feb-17 10:30am    
Afzaal i tried and it didn't help :s
Would u please if u can code me the solution for my case because i am stuck at this and i cannot resolve it i need your help,
i would highly appreciate your help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900