JSON.NET Deserializing Different Property Names [duplicate] - c#

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; }
}

Related

Deserialize the json to c# object [duplicate]

This question already has answers here:
How can I parse a JSON string that would cause illegal C# identifiers?
(3 answers)
Convert JSON with illegal characters in property name [duplicate]
(1 answer)
Closed 2 years ago.
{
"0": {
"no": "tenon",
"title": "ProdtesterTITLE439",
"stock": 12
},
"success": 1
}
I want to desrialize those json.The problem is I cant create class with name 0 in c sharp.i have tried
[JsonObject(Title = "0")]
and
[DataContract(Name ="0")]
Not one of them are worked.
Good news! Your root object doesn't have a name, so you don't need to create a class with that name. 0 is a property of the root object.
Of course, 0 isn't a valid property name in C# either. That's where JsonPropertyAttribute comes in:
public class RootObject
{
[JsonProperty("0")]
public MyData Data {get;set;}
public bool Success {get;set;}
}
public class MyData
{
public int Stock {get;set;}
// other properties
}

How to deserialize only part of the json document? [duplicate]

This question already has answers here:
Deserializing just a single node of a JSON response
(2 answers)
Closed 4 years ago.
Using Json.Net on dotnet core v3 preview.
The json looks similar to:
{
"rootElement": {
"id": 500,
"name": "water balloon"
}
}
I would like to deserialize this into an object that looks like:
public class Item {
public int id {get;set;}
public string name {get;set;}
}
instead of the much more annoying:
public class ItemWrapper {
public Item rootElement {get;set;}
}
This is obviously a contrived example, but it illustrated the problem. The actual Item class is far more complicated. And there are many of them. So I'm trying to identify a solution that will work for any json document that has this general format with a root node followed by the object I am actually interested in.
Any thoughts?
You can use JObject.Parse from Newtonsoft.Json.Linq namespace, and get your item like so:
var obj = JObject.Parse("json");
var item = obj["rootElement"].ToObject<Item>();

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; }
}

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; }

Colon in C# List definition [duplicate]

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; }

Categories