Colon in C# List definition [duplicate] - c#

This question already has answers here:
How can I parse a JSON string that would cause illegal C# identifiers?
(3 answers)
Closed 8 years ago.
I have to deserialize an JSON object array that contains a colon in root object name.
Does anyone know if I can achieve this somehow with colon in list definition?
public List<Customers> ngcp:customers { get; set; }

No, the proper way to do this would be to specify the name using an attribute or other method supported by json.net.
[JsonProperty(PropertyName = "ngcp:customers")]
public List<Customers> Customers { get; set; }

Related

How to print converted object from QuickType.io? [duplicate]

This question already has answers here:
What is the best way to dump entire objects to a log in C#?
(16 answers)
Why writing items to console writes only namespace and class name instead of data? [duplicate]
(5 answers)
Class List Keeps Printing Out As Class Name In Console?
(4 answers)
C#: Printing all properties of an object [duplicate]
(9 answers)
Closed 4 years ago.
I'm trying to find out how QuickType.io works, however I cannot find a way to show my converted results.
I converted a JSON file to C# and got a satisfactory output:
public partial class Welcome
{
[JsonProperty("relatiesoort")]
public string[] Relatiesoort { get; set; }
[JsonProperty("modifiedOn")]
public DateTimeOffset ModifiedOn { get; set; }
[JsonProperty("relatiecode")]
public long Relatiecode { get; set; }
[JsonProperty("naam")]
public string Naam { get; set; }
}
I think the Welcome class can be used to do what I want:
public partial class Welcome
{
public static Welcome[] FromJson(string json) => JsonConvert.DeserializeObject<Welcome[]>(json, APIconnectV2.JSON.Converter.Settings);
}
This is the code I use to print the results:
var result = httpClient.GetAsync(new Uri("http://hidden.com')")).Result;
var json = result.Content.ReadAsStringAsync().Result;
dynamic jObject = JArray.Parse(json);
System.Diagnostics.Debug.WriteLine(Welcome.FromJson(json)[0]);
However, the output I receive is:
APIconnectV2.JSON.Welcome
Any number greater than 0 results in an out of bounds error. I'm not even sure if this is the correct way to print my output, however I cannot find a lot of documentation surrounding QuickType.

Parse json name begins with number in C# by Json.net - Newtonsoft [duplicate]

This question already has answers here:
Deserialize json that has some property name starting with a number
(2 answers)
Closed 5 years ago.
I need to parse JSON file in C# code by using JSON.net (Newtonsoft)
But json file I receive begins as this:
{"3h":3}
the variable name begins with number but c# can't do like this.
How can I set the value in the right way? Should I swap the variable name by my self? That would make very dirty code.
Thank you.
You can do this little focus with mapping:
class Program
{
static void Main(string[] args)
{
string jsonInput = #"{""3h"":3}";
var result = (myJsonObj)JsonConvert.DeserializeObject<myJsonObj>(jsonInput);
Console.WriteLine(result.MyProperty);
}
}
public class myJsonObj
{
[JsonProperty(PropertyName = "3h")]
public string MyProperty { get; set; }
}

write into a text file from a list<T> c# [duplicate]

This question already has answers here:
Saving from List<T> to txt
(7 answers)
Closed 5 years ago.
I am working with c# and I have a list of type list where Family is a class with this structure:
public class Family
{
public Father father;
public Mother mother;
public int IdFamily { get; set; }
}
Father and Mother are also classes with their own attributes.
My list has got data about the families. I want to store all that data from the list into a txt files.
How I can do this using System.IO and StreamWriter?
If you go for Json then it's quite straight forward with Newtonsoft
string json = JsonConvert.SerializeObject(family, Formatting.Indented);
File.WriteAllText(path, json);

Is there a way to decorate an int property so that it serializes as a string? [duplicate]

This question already has answers here:
How to make JSON.Net serializer to call ToString() when serializing a particular type?
(4 answers)
Closed 3 years ago.
Is it possible to create a decorator for a property of a class of type int so it serializes as a string?
I have
public class MyClass
{
[SerializeAsString] //this is what I want
public int StreetCode { get; set; }
}
so when I call
var jsonRequest = JsonConvert.SerializeObject(myClass);
I want it to output the value between quotes rather than as an int without quotes.
This requires a custom converter based on Newtonsoft.Json.Converter to be created.
Then you would use the converter like so
[JsonConverter(typeof(ToStringConverter))]
public int StreetCode { get; set; }

JSON.NET Deserializing Different Property Names [duplicate]

This question already has answers here:
C# JSON.NET convention that follows Ruby property naming conventions?
(4 answers)
.NET NewtonSoft JSON deserialize map to a different property name
(6 answers)
Closed 7 years ago.
Is there an automatic way in Json.NET to automatically map JSON properties from lower case underscore to pascal case no underscore. I know you can add json attributes to each property but is that the only way?
My JSON is like this:
{
"first_name": "Roger",
"last_name": "The Shrubber"
}
Here is the class:
public class Customer
{
public FirstName { get; set; }
public LastName { get; set; }
}

Categories