【问题标题】:get Json Data From URL in C#从 C# 中的 URL 获取 Json 数据
【发布时间】:2018-07-18 05:23:38
【问题描述】:

我是 json 新手,我想从链接中获取 json 数据,这里来自网络搜索我已经编写了代码

    private void button1_Click(object sender, EventArgs e)
    {
        string url = @"http://hololens5.northeurope.cloudapp.azure.com/INTERSHOP/web/WFS/inSPIRED-inTRONICS_Business-Site/en_US/-/USD/ViewProduct-Start?SKU=1599925&CategoryName=151&CatalogID=Computers";
        using (WebClient wc=new WebClient())
        {
                json = wc.DownloadString(url);
        }

        string path = @"ouptputfileJSON.json";

        if (!File.Exists(path))
        {
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine(json);
            }
        }
    }

当我执行此代码时,我会在 html 页面中获得输出。如何在提供的链接中获取所选产品的 json 数据

【问题讨论】:

    标签: json c#-4.0


    【解决方案1】:

    这是您要查找的其余端点:

    http://hololens5.northeurope.cloudapp.azure.com/INTERSHOP/rest/WFS/inSPIRED-
    inTRONICS_Business-Site/-;loc=en_US;cur=USD/products/1599925
    

    关于其他休息端点的文档: https://support.intershop.com/kb/index.php/Display/T27711

    【讨论】:

    • 嗨@Willem Evertse,使用上面的intershop链接,我已经按照link这种格式在Unity中创建了ShoppingCart系统。这个Web Site 链接正在运行网站。当我将产品添加到购物车时,它没有在网站中显示购物车
    • 我不确定我是否理解这个问题,但如果你想使用 intershop 的购物篮休息 api,你需要遵循他们的流程,而不是创建你自己的。有关如何使用其余 API 与 intershop 集成的详细信息,请参阅此 support.intershop.com/kb/index.php/Display/260Q29。如果您对使用 intershop 标签的 intershop 帖子有任何疑问,他们会帮助您。
    【解决方案2】:

    因为

    http://hololens5.northeurope.cloudapp.azure.com/INTERSHOP/web/WFS/inSPIRED-inTRONICS_Business-Site/en_US/-/USD/ViewProduct-Start?SKU=1599925&CategoryName=151&CatalogID=Computers

    是网页而不是 API 端点,因此您需要从要获取数据的位置找到合适的端点

    获得正确的端点后,您可以在下面使用

    这是一个如何使用 httpclient 发出请求的示例

    static void Main()
    {
        Task t = new Task(DownloadPageAsync);
        t.Start();
        Console.WriteLine("Downloading page...");
        Console.ReadLine();
    }
    
    static async void DownloadPageAsync()
    {
        // ... Endpoint
        string page = "request URL";
    
        // ... Use HttpClient.
        using (HttpClient client = new HttpClient())
        using (HttpResponseMessage response = await client.GetAsync(page))
        using (HttpContent content = response.Content)
        {
            // ... Read the string.
            string result = await content.ReadAsStringAsync();
            Console.WriteLine(result);
        }
    }
    

    【讨论】:

    • 很抱歉,我作为开发人员真的是初学者,所以请告诉我如何找到端点或如何创建 Api 端点
    • 是你自己的网站吗?
    • 这是在 .net tutorialsteacher.com/webapi/test-web-api 中制作 Web API 的好教程
    • 你说他们没有合适的终点,这意味着 .html 或 .aspx 或其他任何东西都应该在那里
    • 当您调用 .aspx 或 .html 页面时,它总是会为您提供 html 数据,因为这就是它们的本意。另一方面,API 会根据请求和配置为您提供 JSON/XML 或任何其他格式,因为这就是他们所做的。所以你不能指望来自 aspx 或 html 的 JSON
    猜你喜欢
    • 2020-05-30
    • 2019-01-19
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 2014-03-13
    相关资源
    最近更新 更多