Deserialize JSON with empty string key


Deserialize JSON with empty string key



These are the data contract class and attributes below.


public partial class ValidationErrors
{

[System.Runtime.Serialization.DataMemberAttribute()]
public string Message;

[System.Runtime.Serialization.DataMemberAttribute()]
public ModelState ModelState;
}

[System.Runtime.Serialization.DataContractAttribute()]
public partial class ModelState
{
[System.Runtime.Serialization.DataMemberAttribute()]
public string empty;
}



This is the JSON code where I'm getting the issue and is after the "ModelState" where no field name is given.



How do I create data Contract classes to reflect the JSON below?


{"Message":"The request is invalid.","ModelState":{"":["Some messege is being displayed"]}}



EDIT Code to proccess deserialization:


Stream res= await response.Content.ReadAsStreamAsync();
var x = await response.Content.ReadAsStringAsync();
object result= ModelStateSerializer.ReadObject(res);





class ModelState: Dictionary<string, List<string>> { }
– Nkosi
Jun 30 at 0:39


class ModelState: Dictionary<string, List<string>> { }





Any particular reason you HAVE to use data contracts
– Nkosi
Jun 30 at 0:40





@Nkosi building xamarin app cross server and cannot use web based stuff
– Mark Evans
Jun 30 at 0:46





And what about Json.Net?
– Nkosi
Jun 30 at 0:47





@Nkosi Ive changed the class to ' [System.Runtime.Serialization.DataContractAttribute()] public class ModelState : Dictionary<string, List<string>> { }'
– Mark Evans
Jun 30 at 0:50





1 Answer
1



Using the following class structure


public partial class ValidationErrors {
public string Message { get; set; }
public ModelState ModelState { get; set; }
}

public partial class ModelState : Dictionary<string, List<string>> {

}



The following example demonstrates how to use Json.Net to generate the desired JSON described in the original question.


public static void Main()
{
var errors = new ValidationErrors {
Message = "The request is invalid.",
ModelState = new ModelState {
{ "", new List<string>(){"Some messege is being displayed"} }
}
};

var json = JsonConvert.SerializeObject(errors);

Console.WriteLine(json);
}



Output:


{"Message":"The request is invalid.","ModelState":{"":["Some messege is being displayed"]}}



The output JSON is able to be deserialized to the same class structure used in the serialization.



The following example reads the content of a response and converts it to the desired type.


var json = await response.Content.ReadAsStringAsync();
var errors = JsonConvert.DeserializeObject<ValidationErrors>(json);
var message = errors.Message;
var modelState = errors.ModelState;
var details = modelState[""].FirstOrDefault();





Im actually trying to deserialize.
– Mark Evans
Jun 30 at 1:13





Can't you just do public Dictionary<string, List<string>> ModelState {get; set;}
– cricket_007
Jun 30 at 1:20


public Dictionary<string, List<string>> ModelState {get; set;}





@cricket_007 yes you can do that as well. It will work the same. The OP already had a class structure so I decided to include the class.
– Nkosi
Jun 30 at 1:22





@Nkosi I will consider using Json.net at last resort if i cannot find a solution with data contract. behold my ignorance.
– Mark Evans
Jun 30 at 1:23





@MarkEvans the choice is yours. I just provided you with an option. Hope it helps. Happy coding.
– Nkosi
Jun 30 at 1:24






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift