Answer the question
In order to leave comments, you need to log in
How to print an object to the C# console?
How to output an object to the console after deserialization in C#. I received a response from a GET request, deserialized it using the Newtonsoft library, and I want to somehow check the result, so I'm thinking of outputting it to the console, but I don't know how. Or maybe there are other ways to check. In Visual Studio, it was possible to open an object directly in the ide. But I use rider and don't know if there is an alternative to it.
using System;
using System.IO;
using System.Net;
using QuickDeser;
namespace ConsoleApplication2
{
internal class Program
{
public static void Main(string[] args)
{
WebRequest request = WebRequest.Create("https://opensky-network.org/api/states/all?lamin=51.421812&lomin=23.139124&lamax=55.961184&lomax=33.472494");
WebResponse response = request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
string line = "";
// Console.WriteLine(Welcome.FromJson(line));
var welcome = Welcome.FromJson(line);
Console.WriteLine(welcome);
}
}
response.Close();
Console.WriteLine("Запрос выполнен");
Console.Read();
}
}
}
<code lang="cs">
_________________________________________________________________________
это классы для десcериализации брал с сервиса quicktype
<code lang="cs">
namespace QuickDeser
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class Welcome
{
[JsonProperty("time")] public long Time { get; set; }
[JsonProperty("states")] public List<List<State>> States { get; set; }
}
public partial struct State
{
public bool? Bool;
public double? Double;
public string String;
public static implicit operator State(bool Bool) => new State {Bool = Bool};
public static implicit operator State(double Double) => new State {Double = Double};
public static implicit operator State(string String) => new State {String = String};
public bool IsNull => Bool == null && Double == null && String == null;
}
public partial class Welcome
{
public static Welcome FromJson(string json) =>
JsonConvert.DeserializeObject<Welcome>(json, QuickDeser.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Welcome self) =>
JsonConvert.SerializeObject(self, QuickDeser.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
StateConverter.Singleton,
new IsoDateTimeConverter {DateTimeStyles = DateTimeStyles.AssumeUniversal}
},
};
}
internal class StateConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(State) || t == typeof(State?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
switch (reader.TokenType)
{
case JsonToken.Null:
return new State { };
case JsonToken.Integer:
case JsonToken.Float:
var doubleValue = serializer.Deserialize<double>(reader);
return new State {Double = doubleValue};
case JsonToken.Boolean:
var boolValue = serializer.Deserialize<bool>(reader);
return new State {Bool = boolValue};
case JsonToken.String:
case JsonToken.Date:
var stringValue = serializer.Deserialize<string>(reader);
return new State {String = stringValue};
}
throw new Exception("Cannot unmarshal type State");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
var value = (State) untypedValue;
if (value.IsNull)
{
serializer.Serialize(writer, null);
return;
}
if (value.Double != null)
{
serializer.Serialize(writer, value.Double.Value);
return;
}
if (value.Bool != null)
{
serializer.Serialize(writer, value.Bool.Value);
return;
}
if (value.String != null)
{
serializer.Serialize(writer, value.String);
return;
}
throw new Exception("Cannot marshal type State");
}
public static readonly StateConverter Singleton = new StateConverter();
}
}
</code>
Answer the question
In order to leave comments, you need to log in
1- json is a string, you can just print
2 - if you want to check your class - you can output fields with explanations (make a function). you can override .ToString() for your class
ps use the toolbar to insert tags, because they broke along the way. you managed to achieve alignment, but no backlighting. still hard to read
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question