below is the code where i am not able to write the data to json format in txt/json file. it is adding as a string. please help, how can i add the data to txt/json file from rest api output. what changes is require.
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
namespace ConsoleApp2
{
public class Class1
{
private const string URL = "http://api.aladhan.com/calendar?latitude=51.5073509&longitude=-0.1277583&timezonestring=Europe%2FLondon&method=2&month=03&year=2016";
static void Main(string[] args)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
// List data response.
HttpResponseMessage response = client.GetAsync(URL).Result; // Blocking call!
if (response.IsSuccessStatusCode)
{
// Parse the response body. Blocking!
var dataObjects = response.Content.ReadAsStringAsync().Result ; //.ReadAsAsync<IEnumerable<DataObject>>().Result;
string ab = JsonConvert.SerializeObject(dataObjects.ToString(), Formatting.Indented);
using (StreamWriter file = File.CreateText(@"e:\path.txt"))
{
JsonSerializer serializer = new JsonSerializer();
//serialize object directly into file stream
serializer.Serialize(file, ab);
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
}
}
}