0

Here is a data contract

class A
{
    public string Name{get;set;}
}

We are going to use var obj = JsonConvert.DeserializeObject<A>(jsongString) to deserialize the json string into an object. But the json looks like:

{
    "#Name#": "A"
}

Any change or inheritage are not allowed to class A in A.cs file, such as add attribute [JSONProperty(PropertyName="#Name#")] to property. Is it possible to deserialize the string with a customed deserialize settings, what I want is just like:

var deserializeSetting = new CustomedSettings();
var obj = JsonConvert.DeserializeObject<A>(jsonString, deserializeSetting);
7
  • Is this a Json.net specific question? Or are you looking for any specific solution in .NET? Commented May 13, 2019 at 8:38
  • 1
    If it's Json.net specific here is your dupe : How can I parse a JSON string that would cause illegal C# identifiers? , Convert JSON with illegal characters in property name. And documentation newtonsoft JsonPropertyAttribute name Commented May 13, 2019 at 8:41
  • Thanks for your help, but the answers seems that it need to add JsonProperty attribute to the data contract class, which is not allowed to me. Commented May 13, 2019 at 8:49
  • Our comments were not clear enought. Let me reformulate: Are you using "Json.net", "Newtonsoft Json.net"? - Yes or No?. If you have any requirement that will invalidate any answer please add them to your question using edit button. And please be precise on the "Im not allowed" part. Commented May 13, 2019 at 8:54
  • 2
    The requirement is not enought to invalidate the dupe answer. The solution remain the same just create a class A_bis and add a projection from A_bis to A. It's ok to have a special class to map a Json, then a buissness calss that is the object you will work with. Is that ok? The solution using a dictionarry is also valid, adding a slect to project to the real class. Commented May 13, 2019 at 8:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.