0

Although there are tons of links to convert json to object but here the requirement is bit different.

I have this xml -

{
  "structure": {
    "-type": "DivideByZeroException",
    "property": [
      {
        "-key": "Message",
        "#text": "Attempted to divide by zero."
      },
      { "-key": "Data" },
      { "-key": "InnerException" },
      {
        "-key": "TargetSite",
        "#text": "Void btnWriteError_Click(System.Object, System.Windows.RoutedEventArgs)"
      },
      {
        "-key": "StackTrace",
        "#text": "   at WpfApplication1.MainWindow.btnWriteError_Click(Object sender, RoutedEventArgs e) in c:\\Users\\Anil.Purswani\\Desktop\\WPF_Application\\WpfApplication1\\MainWindow.xaml.cs:line 169"
      },
      { "-key": "HelpLink" },
      {
        "-key": "Source",
        "#text": "WpfApplication1"
      },
      {
        "-key": "HResult",
        "#text": "-2147352558"
      }
    ]
  }
}

and would like to convert it into -

Class ModelError
{
   string Message;
   Exception InnerException;
   string TargetSite;
   string StackTrace;
   string HelpLink;
   string Source;
   string HResult;
}

So the final object should contain like this -

modelerrorobj.Message =  "Attempted to divide by zero."
modelerrorobj.Data = null;
modelerrorobj.InnerException = null;
modelerrorobj.Targetsite = "Void btnWriteError_Click(System.Object, System.Windows.RoutedEventArgs)";
modelerrorobj.StackTrace = "   at WpfApplication1.MainWindow.btnWriteError_Click(Object sender, RoutedEventArgs e) in c:\\Users\\Anil.Purswani\\Desktop\\WPF_Application\\WpfApplication1\\MainWindow.xaml.cs:line 169"

So basically, "-key" value i.e. "Message", "Data", "StackTrace" is the field in class ModelError and "#text" is the value of that corresponding field.

for e.g.

      {
        "-key": "Message",
        "#text": "Attempted to divide by zero."
      },

in above, "Message" is the field and "Attempted to divide by zero." is the value of that field.

Please note I have Json but how to convert it in ModelError?

Anil Purswani
  • 1,857
  • 6
  • 35
  • 63

1 Answers1

1

Checkout Json.net Custom Converter: http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConverter.htm

Below is basic idea, you could complete it:

class MyCustomConverter : JsonConverter
{
    public override Object ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
    {
        var jobj = JObject.Load(reader);
        var model = new ModelError();
        var array = jobj.Value<JArray>("property");
        foreach(var item in array)
        {
            switch(item.Value<string>("-key"))
            {
                case "Message":
                    // Should checking if item["#value"] null
                    model.Message = item.Value<string>("#value");
                    break;
                case etc://write your code
            }
        }
        return model;
    }
}


[JsonConverter(typeof(MyCustomConverter))]
Class ModelError
{
   string Message;
   Exception InnerException;
   string TargetSite;
   string StackTrace;
   string HelpLink;
   string Source;
   string HResult;
}

class Container
{
    public ModelError ModelError {get;set;}
} 

var modelError = JsonConvert.DeserializeObject<Container>(json);

// I don't have VisualStudio right now, so there may contains typo error

Nguyen Kien
  • 1,897
  • 2
  • 16
  • 22