使用c#从url保存json文件 [英] save json file from url using c#

查看:193
本文介绍了使用c#从url保存json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要将JSON文件保存到本地计算机。获取这些文件的唯一方法是通过URL,而不是Web服务或FTP。

我以前没有使用过JSON的经验。我已经找到了将JSON转换为sql或CSV的方法,但是如何从url读取它然后转换。



我的问题是:如何将文件保存到我的使用C#或javascript将本地计算机(和/)转换为SQL Server表或excel表。



想要下载和转换的JSON文件示例:

http://abc.com/json/models.json?makeid=make1



任何帮助都将非常感谢



谢谢,

Samira

Hi all,

I need to save JSON files to my local machine. The only way to get those files is via URL, not a webservice or FTP.
I'm really have no experience with working with JSON before. I have found ways to convert JSON to sql or CSV, but how to read it from url and then convert.

My question is: how to save the file to my local machine (and or) convert it to SQL server table or excel sheet using C# or maybe javascript .

Example of JSON file want to download and convert:
http://abc.com/json/models.json?makeid=make1

Any help will be so much appreciated

thanks,
Samira

推荐答案

问题没有正确制定,因为通过URL 不是网络服务或FTP的替代品,因为这些是不同级别的概念,URL可以是Web服务,FTP,任何东西,而你没有告诉我们什么。



但是,因为答案对于协议或某种类型的Internet资源来说不是太具体,所以你可以得到它。此外,文件类型完全不相关;你可以用完全相同的方式下载任何一个。您可以使用其中一个类 System.Net.WebClient System.Net.WebRequest

https://msdn.microsoft.com /en-us/library/system.net.webrequest%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.net.webclient %28v = vs.110%29.aspx [ ^ ],

另见 https://msdn.microsoft.com/en-us/library/ez801hhe%28v=vs.110%29.aspx [ ^ ]。



班级 WebRequest 是抽象的,但具体的非抽象类型由作为参数传递给其工厂方法的URI自动确定 创建

https:// msdn .microsoft.com / zh-CN / library / system.net.webrequest.create%28v = vs.110%29.aspx [ ^ ]。



您不必将JSON下载到文件中,尤其是在下载的最终目标是数据库或Excel文档时。至于您的进一步问题,答案取决于您希望如何将JSON内容映射到输出。



使用Excel,您可以使用Microsoft Open XML SDK。此CodeProject文章将为您提供一个有用的代码示例:创建基本的Excel工作簿Open XML [ ^ ]。



另请参阅本文中引用的过去答案:如何从MS Visual Studio 2010中的添加引用添加microsoft excel 15.0对象库 [ ^ ]。



有关JSON解析的更多常规问题和可能的转换,请看我过去的答案:

http://www.codeproject.com/Answers/805340/How-To-Convert-object-type-to-Csharp-class-object#answer1 [ ^ ],

如何将多级json数据转换为C#对象? [ ^ ],

从Cloudant(json文档)获取数据 [ ^ ]。br />


-SA
The problem is not properly formulated, because "via URL" is not an alternative to "a webservice or FTP", because those are notions of different levels, and URL can be of a Web service, of FTP, of anything, and you did not tell us what.

But, because the answer would not be too specific to the protocol or a type of Internet resource, you can get it. Also, the file type is totally irrelevant; you can download any in exact same way. You can use one of the classes System.Net.WebClient or System.Net.WebRequest:
https://msdn.microsoft.com/en-us/library/system.net.webrequest%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.110%29.aspx[^],
see also https://msdn.microsoft.com/en-us/library/ez801hhe%28v=vs.110%29.aspx[^].

The class WebRequest is abstract, but a concrete non-abstract type is determined automatically by the URI passed as a parameter to its factory methods Create:
https://msdn.microsoft.com/en-us/library/system.net.webrequest.create%28v=vs.110%29.aspx[^].

You don't have to download JSON to a file, especially if the final target of your downloading is the database or an Excel document. As to your further questions, the answer depends on how you want to map your JSON content to your output.

With Excel, you can use Microsoft Open XML SDK. This CodeProject article will give you a useful code sample: Creating basic Excel workbook with Open XML[^].

See also my past answers referenced in this one: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

For more general issues related to JSON parsing and possible conversions, please see my past answers:
http://www.codeproject.com/Answers/805340/How-To-Convert-object-type-to-Csharp-class-object#answer1[^],
how to conver multi level json data to C# Object?[^],
haw to get data from Cloudant (json document)[^].

—SA


感谢@Sergy Sergey Alexandrovich Kryukov帮我出去了。

发布我的解决方案来帮助他人。



Thanks to @Sergy Sergey Alexandrovich Kryukov who helped me out.
Posting my solution to help others.

//*********get the json file using httpRequest ***********

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://abc.ca/makes.json");
            httpWebRequest.Method = WebRequestMethods.Http.Get;
            httpWebRequest.Accept = "application/json; charset=utf-8";
            string file;
            var response=(HttpWebResponse)httpWebRequest.GetResponse();
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                file = sr.ReadToEnd();
            }




//********** using using Newtonsoft.Json to Deserialize ****** 

            var table = JsonConvert.DeserializeAnonymousType(file, new { Makes = default(DataTable) }).Makes;
            if (table.Rows.Count > 0)
            {
                //do something 
            }


这篇关于使用c#从url保存json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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