C# 通过网页Json内容获取在线时间

// 参考 :
//   读取网页文本: https://blog.csdn.net/Salted___Fish__/article/details/108493530#:~:text=%40C%23%20%E9%80%9A%E8%BF%87URL%E8%AF%BB%E5%8F%96%E7%BD%91%E7%BB%9C%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9%20%2F%2F%E7%BD%91%E7%BB%9C%E6%96%87%E4%BB%B6%E5%9C%B0%E5%9D%80%20string%20file_url%20%3D%20%40%22https%3A%2F%2Fwww......%22%3B%20%2F%2F%E5%AE%9E%E4%BE%8B%E5%8C%96%E5%94%AF%E4%B8%80%E6%96%87%E4%BB%B6%E6%A0%87%E8%AF%86,%3D%20new%20Uri%28file_url%29%3B%20%2F%2F%E8%BF%94%E5%9B%9E%E6%96%87%E4%BB%B6%E6%B5%81%20Stream%20stream%20%3D%20WebRequest.Create%28file_uri%29.GetResponse%28%29.GetResponseStream%28%29%3B
//   网络时间: https://www.nowapi.com/api/life.time
//   读取josn格式: https://zhuanlan.zhihu.com/p/613110325
using Newtonsoft.Json.Linq;  // 需要在NuGet下载NewtonJson库
using Newtonsoft.Json;         // 需要在NuGet下载NewtonJson库
using System.Net;

namespace GethttpTime
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //网络文件地址
            string Web_Uri = @"https://sapi.k780.com/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
            
            //实例化唯一文件标识
            Uri file_uri = new Uri(Web_Uri);
          
            //返回文件流
            Stream stream = WebRequest.Create(file_uri).GetResponse().GetResponseStream();
           
            //实例化文件内容
            StreamReader file_content = new StreamReader(stream);
           
            //读取文件内容
            string file_content_str = file_content.ReadToEnd();

            // josn 读取文本并格式化
            JObject objs = JObject.Parse(file_content_str);

            //输出过滤内容
            Console.WriteLine(objs["result"]["datetime_1"]);
        }
    }
}

 

posted @ 2024-04-04 04:46  edolf  阅读(35)  评论(0编辑  收藏  举报