1

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); 
thomsan
  • 433
  • 5
  • 19
  • The value of `"JsonFile"` is embedded, double-serialized JSON, so you can use `EmbeddedLiteralConverter` from [How do I convert an escaped JSON string within a JSON object?](https://stackoverflow.com/a/39154630/3744182). – dbc Aug 11 '18 at 18:41
  • @dbc Hello sir, I'm new to c# and JSON, please give me sample code to solve my problem. please – thomsan Aug 12 '18 at 02:58
  • Just deserialize to your `Response` type with `new JsonSerializerSettings { Converters = { new EmbeddedLiteralConverter() } }` as shown in https://dotnetfiddle.net/LuNdkf – dbc Aug 12 '18 at 18:27
  • @dbc Thank you sir, I again have a problem. please help me to solve it. this is my question : https://stackoverflow.com/questions/51845381/how-to-escape-embedded-json-after-unescape – thomsan Aug 14 '18 at 15:54

0 Answers0