从C#中的URL获取Json数据

时间:2018-07-18 05:23:38

标签: json c#-4.0

我是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数据

2 个答案:

答案 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

答案 1 :(得分:0)

因为

  

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