Get json data from url and output as a csv file - c#

How can you get JSON data dynamically, then output it as a CSV file? Is it possible to get it to output with a change in data?
I have the following code below, but it doesn't work.
var url = "https://reqres.in//api/users?page=2";
using (WebClient webClient = new System.Net.WebClient())
{
WebClient n = new WebClient();
var json = n.DownloadString(url);
string valueOriginal = Convert.ToString(json);
}
I want to get the JSON data from whatever URL and output it as a CSV.

Can you just use CsvHelper? One example can be found from here.
Another option is to write your own implementation to achieve this goal (as shown in example1 or example2).
Good luck.

Related

How to download a file from a URL which is passed as a query string?

I am passing a URL which has another URL in the query string. I have shown an example below:
https://www.aaa.com/triBECKML/kmlHelper.htm?https://lkshd.ty.etys.nux/incoming/triBEC/final_year_base_data/KMLS/NetAverages.kml
I have tried a WebClient to download the file but it only downloads an empty .kml. Additionally when I call the method with just the 2nd URL (IN THE QUERY STRING), the file gets downloaded smoothly.
using (var client = new WebClient())
{
client.DownloadFile(url, destinationPath);
}
You can try to split the string. And use only the second part?
Example:
string[] urls = url.Split('?');
using (var client = new WebClient())
{
client.DownloadFile(urls[1], destinationPath);
}
This splits the url string and puts the first url and the second in a array. Then you can use the url you want.

How do I grab specific strings from a source?

I want to create an app that shows current information, i can get the information using a simple (at least, it looks pretty simple) API.
Now, you can use the API by going to the website, and enter the username. The URL will result in something like this site.web/api/public/user?name=Usename.
on that page is all the information I need, in form of one line of 'code'.
{"uniqueId":"hhus-7723dec98ecb9bc6643f10588e0bb3f4","name":"Username","figureString":"hr-125-40.hd-209-1369.ch-210-64.lg-270-1408.he-3329-1408-1408","selectedBadges":[],"motto":"sample txt","memberSince":"2012-08-25T14:01:04.000+0000","profileVisible":true,"lastWebAccess":null}
I want to extract this information and display it in my program, example:
{"uniqueId":"this is an ID"}
I only want the actual ID to be shown: this is an ID.
Thanks for helping!
The format you're receiving is called JSON. There are lots of libraries to read it easily, the most widely used in C# is JSON.NET.
If you only need to extract one property, you can do something like this:
string json = ...
var obj = JObject.Parse(json);
string uniqueId = obj["uniqueId"].Value<string>();
If you also need the other properties, it's probably easier to use deserialization: create a class with the same properties as the JSON object, and use JsonConvert.DeserializeObject to read the JSON into an instance of the class.
The one line of code you're referring to is JSON data. It's stored in the format "key":"value","key:value","key:value" and so on.
You should take a look at Newtonsoft.Json which helps you do exactly this: parse JSON data :)
https://www.nuget.org/packages/Newtonsoft.Json/
Tying it all together for you...
using System.IO;
using System.Net;
using Newtonsoft.Json.Linq;
...
WebClient client = new WebClient();
Stream stream = client.OpenRead("http://site.web/api/public/user?name=Usename");
StreamReader reader = new StreamReader(stream);
string userJson = reader.ReadLine();
reader.Close();
JObject jObject = JObject.Parse(userJson);
string uniqueId = (string)jObject["uniqueId"];
This is an example of Json. The most type safe way if to deserialize the data to a class you define.
Such a class could look like this:
public class MyClass
{
public string uniqueId { get; set; }
}
If you have the data in a string you can just deserialize it with the Newtonsoft.Json nuget package.
MyClass obj = JsonConvert.Deserialize<MyClass>(myJsonString);
If you get the data from http it is easier to use an client which can do the deserialization for you. Such a client is found in the nuget package Microsoft.AspNet.WebApi.Client
using(var client = new HttpClient())
{
var response = await client.GetAsync(myUrl);
response.EnsureSuccessStatusCode();
MyClass obj = await response.Content.ReadAsAsync<MyClass>();
}
Of course this assumes the server is standards compliant and specifies it's content-type as application/json
Bonus: The classes you deserialize to can be auto generated from example at the site: http://json2csharp.com/ .

Consuming web services in C#

I just started playing around with some API's in C#. In my form I had added a service reference http://wsf.cdyne.com/WeatherWS/Weather.asmx. Everything works great and I am able to utilize its library. Now I am trying to use for example http://free.worldweatheronline.com/feed/apiusage.ashx?key=(key goes in here)&format=xml. [I have a key] Now when I try to use it as service reference I am not able to use.
Do I have to call it in my form instead of referencing it? or do some sort of conversion? Also does it matter if its xml or json type?
ASMX is old technology and uses SOAP under the hood. SOAP doesn't tend to work with query string parameters, it takes parameters as part of the message.
ASHX is something different (it could be anything, it's one way to write a raw HTML/XML page in .NET), so you can't transfer the method for calling one to the other. It also won't have a service reference, it's likely you request it via a raw HTTP request. You'll need to consuly the service documentation to discover how to use it.
worldweatheronline doesn't return a SOAP-XML that is consumable by a WebService client. Therefore you should download the response and parse it as done with many REST services.
string url = "http://free.worldweatheronline.com/feed/apiusage.ashx?key=" + apikey;
using (WebClient wc = new WebClient())
{
string xml = wc.DownloadString(url);
var xDoc = XDocument.Parse(xml);
var result = xDoc.Descendants("usage")
.Select(u => new
{
Date = u.Element("date").Value,
DailyRequest = u.Element("daily_request").Value,
RequestPerHour = u.Element("request_per_hour").Value,
})
.ToList();
}
Also does it matter if its xml or json type?
No, at the end you have to parse the response by yourself.
string url = "http://free.worldweatheronline.com/feed/apiusage.ashx?format=json&key=" + apikey;
using (WebClient wc = new WebClient())
{
string json = wc.DownloadString(url);
dynamic dynObj = JsonConvert.DeserializeObject(json);
var jArr = (JArray)dynObj.data.api_usage[0].usage;
var result = jArr.Select(u => new
{
Date = (string)u["date"],
DailyRequest = (string)u["daily_request"],
RequestPerHour = (string)u["request_per_hour"]
})
.ToList();
}
PS: I used Json.Net to parse the json string

Howto: download a file keeping the original name in C#?

I have files on a server that can be accessed from a URL formatted like this:
http:// address/Attachments.aspx?id=GUID
I have access to the GUID and need to be able to download multiple files to the same folder.
if you take that URL and throw it in a browser, you will download the file and it will have the original file name.
I want to replicate that behavior in C#. I have tried using the WebClient class's DownloadFile method, but with that you have to specify a new file name. And even worse, DownloadFile will overwrite an existing file. I know I could generate a unique name for every file, but i'd really like the original.
Is it possible to download a file preserving the original file name?
Update:
Using the fantastic answer below to use the WebReqest class I came up with the following which works perfectly:
public override void OnAttachmentSaved(string filePath)
{
var webClient = new WebClient();
//get file name
var request = WebRequest.Create(filePath);
var response = request.GetResponse();
var contentDisposition = response.Headers["Content-Disposition"];
const string contentFileNamePortion = "filename=";
var fileNameStartIndex = contentDisposition.IndexOf(contentFileNamePortion, StringComparison.InvariantCulture) + contentFileNamePortion.Length;
var originalFileNameLength = contentDisposition.Length - fileNameStartIndex;
var originalFileName = contentDisposition.Substring(fileNameStartIndex, originalFileNameLength);
//download file
webClient.UseDefaultCredentials = true;
webClient.DownloadFile(filePath, String.Format(#"C:\inetpub\Attachments Test\{0}", originalFileName));
}
Just had to do a little string manipulation to get the actual filename. I'm so excited. Thanks everyone!
As hinted in comments, the filename will be available in Content-Disposition header. Not sure about how to get its value when using WebClient, but it's fairly simple with WebRequest:
WebRequest request = WebRequest.Create("http://address/Attachments.aspx?id=GUID");
WebResponse response = request.GetResponse();
string originalFileName = response.Headers["Content-Disposition"];
Stream streamWithFileBody = response.GetResponseStream();

Reading from JSON, data not displayed

I'm having this strange problem when i try to access elements in json from javascript. i retreve a json string from a url like so,
// Create Request
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(#"www.someurl.com");
// Create Client
WebClient client = new WebClient();
// Assign Credentials
client.Credentials = new NetworkCredential("username", "pass");
// Grab Data
sjson = client.DownloadString(#"www.someurl.com");
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
oSerializer.MaxJsonLength = Int32.MaxValue;
sjson = oSerializer.Serialize(sjson);
But when i access this sjson variable from javascript in html code, it doesn't return anything, but if i hard code it, it returns the values, please help on this one. I tried many things but didn't work. I also tried to just pass the retreieved json string without serializing, when i do that the javascript stops working. :( following is the javascript code,
<script type="text/javascript">
var jsons = JSON.parse('<%=sjson%>');
function initialize() {
alert("hahahahaaa");
document.writeln(jsons.Body[0].RowId.SensorIdValue);
//document.writeln(myobject.Body[0].RowId.SensorIdValue);
}
</script>
The issue is
document.writeln(myobject.Body[0].RowId.SensorIdValue);
returns a value if i use the myobject variable, but
document.writeln(jsons.Body[0].RowId.SensorIdValue);
returns nothing when i use it with the parsed value. :(
following is the sample of the json output (response.write) i get after running the serializer via c#,
Please help me on this one..i cant seem to find the problem here.
EDIT:
if it help, the fiollowing is the json string i get straight from the server witout making any serializations,
Few contents of the question have been removed due to owner request
What you're seeing there is doubly JSON-serialized data. You retrieved JSON from the remote server and then JSON encoded it a second time with JavaScriptSerializer. This is a post I wrote about that, in the context of ASMX ScriptServices, which explains in more detail: http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/. Your case isn't exactly the same, but the end result is.
Remove the JavaScriptSerializer code and return the JSON string you retrieved (sjson) instead of serializing it a second time.
You don't need to use a Json serializer because the remote server already returns a JSON encoded string that you could directly use in your page and avoid the double encoding.
So:
public string GetJson()
{
// Create Client
using (WebClient client = new WebClient())
{
// Assign Credentials
client.Credentials = new NetworkCredential("username", "pass");
// Grab Data
return client.DownloadString(
#"www.someurl.com"
);
}
}
and then:
<script type="text/javascript">
var jsons = <%= GetJson() %>;
function initialize() {
alert("hahahahaaa");
document.writeln(jsons.Body[0].RowId.SensorIdValue);
//document.writeln(myobject.Body[0].RowId.SensorIdValue);
}
</script>

Categories

Resources