How to serialize this C# return value into a JSON object?

后端 未结 1 902
星月不相逢
星月不相逢 2021-01-25 03:29

I have this object value that is being returned and I would like to convert it into a useful JSON object that I can inspect and manipulate. Ultimately, my goal is to validate th

相关标签:
1条回答
  • 2021-01-25 03:50

    Since you cannot fix the source, you're going to have to apply a bodge to fix the JSON, for example this will work:

    var fixedJson = sourceJson.Substring(1, Json.Length - 2);
    

    Now you should have a couple of classes to hold your data, this way you can also cope with the unusual names:

    public class Root
    {
        public string BrowserName { get; set; }
        public string BrowserVersion { get; set; }
        public string PlatformName { get; set; }
    
        [JsonProperty("sauce:options")]
        public Options SauceOptions { get; set; }
    }
    
    public class Options
    {
        public string Username { get; set; }
        public string AccessKey { get; set; }
    }
    

    And now you should be able to deserialise like this:

    var root = JsonConvert.DeserializeObject<Root>(fixedJson);
    
    0 讨论(0)
提交回复
热议问题