简体   繁体   中英

Deserialize JSON object into type System.Guid

What I have is a REST response in JSON format that looks like this : { "guid": "c75d06a8-a705-48ec-b6b3-9076becf20f4" }

When trying to deserialize this reponse String into an Object of type System.Guid like this: Newtonsoft.Json.JsonConvert.DeserializeObject(response.content, type); , The following exception is thrown :

Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Nullable`1[System.Guid]' because the type requires a JSON primitive value (eg string, number, boolean, null) to deserialize correctly. To fix this error either change the JSON to a JSON primitive value (eg string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'guid', line 1, position 8.

Any help is appreciated!

If you don't want to create a class just to contain the Guid value, you can just parse the value directly from the parsed Json:

string json = "{ \"guid\": \"c75d06a8-a705-48ec-b6b3-9076becf20f4\" }";
var container = JToken.Parse(json);
Guid guid;
if (Guid.TryParse(container["guid"]?.ToString(), out guid))
{
    Console.WriteLine(guid);    
}
else{
    Console.WriteLine("No guid present or it has an invalid format");
}
// c75d06a8-a705-48ec-b6b3-9076becf20f4

Another option is to use a dynamic variable, though personally I don't think this is a good use-case for dynamic:

string json = "{ \"guid\": \"c75d06a8-a705-48ec-b6b3-9076becf20f4\" }";
dynamic container = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
Guid guid;
if (Guid.TryParse(container.guid?.ToString(), out guid)) {
    Console.WriteLine(guid);
}
else {
    Console.WriteLine("No guid present or it has an invalid format");
}
// c75d06a8-a705-48ec-b6b3-9076becf20f4

Its been a bit since I've done this type of thing, however I think the answer is in your error message there. Let it come back as string. In your class override the guid string property to set another property of type GUID and perform your conversion and error handling there. Another way is to use a constructor to help out here. Hope this gives you a direction.

Fully example with GUID and Json

string content = "{\"data\":{\"Type\":\"Foo\",\"Description\":\"bar \",\"Software\":\"baz\",\"Version\":\"qux\",\"Url\":\"http://example.com\",\"DatabasePortNumber\":1,\"Id\":\"2cdc66f1-0000-0000-39ac-233c00000000\"}}";

var result_SystemText = System.Text.Json.JsonSerializer.Deserialize<SystemApplicationResponse>(content);  //Will result in null-object
var result_Newtonsoft = Newtonsoft.Json.JsonConvert.DeserializeObject<SystemApplicationResponse>(content); //Will result in correctly serialized object

    public class SystemApplicationResponse
    {
        public SystemApplicationDto Data { get; set; }
    }


    public class SystemApplicationDto
    {
        public SystemApplicationDtoType Type { get; set; }

        public string Description { get; set; }

        public string Software { get; set; }

        public string Version { get; set; }
        public string Url { get; set; }

        public int DatabasePortNumber { get; set; }
        public System.Guid Id { get; set; }
    }

    public enum SystemApplicationDtoType
    {
        Foo = 0
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM