简体   繁体   English

Newtonsoft.Json 从网页解析 json 不起作用

[英]Newtonsoft.Json parsing json from webpage doesn't work

I've made a simple program that gets json from a website.我制作了一个从网站获取 json 的简单程序。 It should give out some of the json attributes, but it doesn't do it.它应该给出一些 json 属性,但它没有这样做。 It simply gives me a clear String without text in it.它只是给了我一个没有文本的清晰字符串。 Can someone help me?有人能帮我吗?

My Code:我的代码:

using System;
using System.Linq;
using System.Diagnostics;
using System.Threading;
using System.Net;
using System.IO;
using Newtonsoft.Json;
namespace ESEL_Scraper_2._0
{
    
    class MyJsonType
    {
        public string title { get; set; }

        public int id { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            WebClient client = new WebClient();
            client.Encoding = System.Text.Encoding.UTF8;
            string site = client.DownloadString($"https://esel.at/api/termine/data?date=05.09.2020&selection=false");
            var myJsonObject = JsonConvert.DeserializeObject<MyJsonType>(site);
            Console.WriteLine(myJsonObject.title);
        }
    }
}

The JSON: https://esel.at/api/termine/data?date=05.09.2020&selection=false JSON: https : //esel.at/api/termine/data? date = 05.09.2020 & selection =false

MyJsonType expects the body to be like, MyJsonType期望身体像,

{
   "id": 1,
   "title": "title"
}

but in your case, the json is,但在你的情况下,json是,

{
   "termine": [
    { -> object }
    { -> object }
   ]
}

You have an array of Objects that fit into your MyJsonType .您有一个适合您的MyJsonType的对象数组。 Use the following class to deserialize to使用以下类反序列化为

public class RootObject 
{
   [JsonProperty("termine")] // the name of the property is case sensetive.
   public List<MyJsonType> Termine {get;set;}
}

public class MyJsonType
{
    [JsonProperty("title")]
    public string Title { get; set; } //C# uses uppercase first letter for properties.
    [JsonProperty("id")]
    public int Id { get; set; }
}

// in your main,
var obj = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(obj.Termine.First().Title);

You need to structure your call as per your json string.您需要根据您的 json 字符串构建您的调用。

You class should look like this你的班级应该是这样的

public class ParentJsonType
        {
            public List<MyJsonType> termine { get; set; }
        }
        public class MyJsonType
        {
            public string title { get; set; }

            public int id { get; set; }
        }

and in your code you can deserialize在你的代码中你可以反序列化

WebClient client = new WebClient();
            client.Encoding = System.Text.Encoding.UTF8;
            string site = client.DownloadString($"https://esel.at/api/termine/data?date=05.09.2020&selection=false");
            var myJsonObject = JsonConvert.DeserializeObject<List<MyJsonType>>(site);
            foreach(var myJosnType in myJsonObject)
            {
                //add your logic here
                Console.WriteLine(myJosnType.title);
            }

Yes, guys that already answered are absolutely right.是的,已经回答的人是绝对正确的。 The point is in the wrong model you have, which has no necessary structure to contain a responded context.关键在于您拥有错误的模型,它没有必要的结构来包含响应的上下文。

The following approach is also working.以下方法也有效。 The correct model for that JSON should be:该 JSON 的正确模型应该是:

public class Termine
{
    public Int64 id {get;set;}//":106102,
    public String title {get;set;}//":"curated by 2020",
    public String category {get;set;}//":"eSeLs Neugierde",
    public String startdate {get;set;}//":"Sa, 05.09.2020",
    public String startdatetime {get;set;}//":"Sa, 05.09. 11:00",
    public String starttime {get;set;}//":"11:00",
    public String enddate {get;set;}//":"Sa, 05.09.2020",
    public List<object> runtime {get;set;}//":[            0,            "nur noch heute"         ],
    public String thumbnail {get;set;}//":"https:\/\/static.esel.at\/mini\/\/upload\/IpeP5wgYL7sRucTuFtpJg53zgG7hby5IiXv5txLk.jpeg",
    public String  thumbnail_credits {get;set;}//":null,
    public String type {get;set;}//":"upcoming",
    public String recommended {get;set;}//":"(Wie) Schafft's Kunst als Aktivismus in Galerier\u00e4ume?`",
    public Boolean online_event {get;set;}//":false,
    public String feed_urls {get;set;}//":null,
    public String status {get;set;}//":"",
    public String tags {get;set;}//":"Galerienfestival, internationale KuratorInnen, Aktivismus, Kunst, curatedby",
    public String url {get;set;}//":"https:\/\/esel.at\/termin\/106102\/curated-by-2020",
    public DateTime sort_date {get;set;}//":"2020-09-05 11:00:00",
    public String sort_category {get;set;}//":"eselsneugierde",
    public String location_url {get;set;}//":"https:\/\/esel.at\/location\/933\/wien",
    public String location {get;set;}//":"Wien"
    public override String ToString(){
        return String.Format("[{0}] {1} ({2})", this.id, this.title, this.location_url);
    }
}
public class Meta
{
    public List<String> next {get;set;}//":[         "Sonntag,<br><nobr>06. September 2020<\/nobr>",         "06.09.2020",         "06092020"      ],
    public DateTime now {get;set;}//":"2020-09-05T18:52:05.000040Z",
    public List<String> da {get;set;}//":[         "Samstag,<br><nobr>05. September 2020<\/nobr>",         "05.09.2020",         "05092020"      ],
    public DateTime end {get;set;}//":"2020-09-05T21:59:59.999999Z",
    public DateTime runtime {get;set;}//":"2020-09-04T22:00:00.000000Z",
    public Int32 upcoming {get;set;}//":14
    public Int32 running {get;set;}//":87,
    public Int64 termine {get;set;}//":16
}
public class Context
{
    public List<Termine> termine{get;set;}
    public Meta meta {get;set;}
}

So, your code will work with a few changes:因此,您的代码将进行一些更改:

public static void Main(string[] args)
    {
        WebClient client = new WebClient()
        {
            Encoding = System.Text.Encoding.UTF8
        };
        string site = client.DownloadString("https://esel.at/api/termine/data?date=05.09.2020&selection=false");
        Context ctx = JsonConvert.DeserializeObject<Context>(site);
        ctx.termine.ForEach(Console.WriteLine);
    }

Here is the link to the full solution where you can run and test.这是完整解决方案的链接,您可以在其中运行和测试。 https://dotnetfiddle.net/elq5Lv https://dotnetfiddle.net/elq5Lv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM