如何从C#中的url反序列化json数组? [英] How do I deserialize json arrays from url in C# ?

查看:132
本文介绍了如何从C#中的url反序列化json数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图从wordpress url反序列化json数据但是没有用。

我看过很多教程,但没有人在我的工作!



这是我的网址: https:// mahdii。 000webhostapp.com/?wpapi=get_posts&dev=1 [ ^ ]



我在var serialized_post中得到Null。



我需要帮助



我尝试过:



我试图使用此代码反序列化:



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);



我的分类:

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&amp;amp;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; }
    }
}

推荐答案

一个原因:因为你没有正确地做到的。您将其反序列化为帖子列表。而您的JSON是一个包含帖子的文档。我可以写一些简单的东西,

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,
// 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.



现在,问题主要在于,当数据来自API时,您需要将其映射回对象的完全相同副本,或者您不能将它映射到任您可能还看到我使用了发布,而不是帖子。那是因为,在这种情况下,案件也很重要。在从API下载数据的情况下,建议始终使用动态,因为您可能不知道正在发送什么和不发送什么,然后在运行时检查它; 动态跳过编译时间类型检查,但为了确保数据存在,我们使用 if ... else 分支加载数据,或显示如果数据不存在,则显示错误消息。



有关此内容的更多信息,请阅读我的文章使用C#从JSON中的零到英雄 [ ^ ],特别是文章底部的答案,专门讨论这个问题。


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.


这篇关于如何从C#中的url反序列化json数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆