I need to bind the JSON object to model classes. To achieve that I used following code lines.
My tested JSON as follows
{
"Message":null,
"Error":false,
"VData":{
"RNumber":null,
"BRNumber":"Session1"
},
"onlineFields":{
"CCode":"Web",
"MNumber":"15478655",
"Product":"100",
"JsonFile":{
"evaluation":{
"number":[
{
"@paraID":"1000",
"@Value":"",
"@label":"We are america"
},
{
"@paraID":"2000",
"@Value":"100",
"@label":"We are japan"
},
{
"@paraID":"3000",
"@Value":"1000",
"@label":"We are UK"
},
{
"@paraID":"4000",
"@Value":"",
"@label":"We are China"
}
]
}
}
}
}
and this is my model classes.
public class VData
{
public object RNumber { get; set; }
public string BRNumber { get; set; }
}
public class Number
{
[JsonProperty("@paraID")]
public string paraID { get; set; }
[JsonProperty("@Value")]
public string Value { get; set; }
[JsonProperty("@label")]
public string label { get; set; }
}
public class Evaluation
{
public List<Number> number { get; set; }
}
public class JsonFile
{
public Evaluation evaluation { get; set; }
}
public class OnlineFields
{
public string CCode { get; set; }
public string MNumber { get; set; }
public string Product { get; set; }
public JsonFile JsonFile { get; set; }
}
public class Response
{
public object Message { get; set; }
public bool Error { get; set; }
public VData VData { get; set; }
public OnlineFields onlineFields { get; set; }
}
To set the above JSON to my model, I used the following code
private static void showJSON(string testJson){
Response response = JsonConvert.DeserializeObject<Response>(testJson);
var dropdowns = response.OnlineFields.JsonFile;
string json = JsonConvert.SerializeObject(dropdowns, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(json);
}
This working fine. but the problem is my client sends me the following JSON through the API.
{
"Message":null,
"Error":false,
"VData":{
"RNumber":null,
"BRNumber":"Session1"
},
"onlineFields":{
"CCode":"Web",
"MNumber":"15478655",
"Product":"100",
"JsonFile":" {
\"evaluation\":{
\"number\":[
{
\"@paraID\":\"1000\",
\"@Value\":\"\",
\"@label\":\"We are america\"
},
{
\"@paraID\":\"2000\",
\"@Value\":\"100\",
\"@label\":\"We are japan\"
},
{
\"@paraID\":\"3000\",
\"@Value\":\"1000\",
\"@label\":\"We are UK\"
},
{
\"@paraID\":\"4000\",
\"@Value\":\"\",
\"@label\":\"We are China\"
}
]
}
} "
}
}
So how to escape this "\" characters and how to bind the client sent JSON to my model. In the client's JSON have inner JSON called JsonFile. This inner JSON consists with "\" and inner JSON consists within quotation "". So please give me a solution.
Updated:
According to this post. I have tried this. but this makes me errors. So please give me a solution. In order to that post or any other way how to solve this
dynamic obj = JsonConvert.DeserializeObject(json);
foreach (var response in (IEnumerable<dynamic>)obj.onlineFields)
{
response.onlineFields.JsonFile = JsonConvert.DeserializeObject((string)response.onlineFields.JsonFile);
}
string result = JsonConvert.SerializeObject(obj);
Console.WriteLine(result);