View JsonParseConsole.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var jsonString = CreateMockJsonString(); | |
var isJsonFormat = jsonString.JsonTryParse<BaseModel<User>>(out var jsonResult); | |
if (!isJsonFormat) | |
{ | |
Console.WriteLine($"{jsonString} is not JSON format !"); | |
} |
View User.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class User | |
{ | |
[JsonPropertyName("Name")] | |
public string Name { get; set; } | |
[JsonPropertyName("Age")] | |
public int Age { get; set; } | |
} |
View BaseModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseModel<T> where T : class | |
{ | |
[JsonPropertyName("Code")] | |
public int Code { get; set; } = -1; | |
[JsonPropertyName("Msg")] | |
public string Message { get; set; } | |
[JsonPropertyName("Response")] | |
public T Response { get; set; } | |
} |
View JsonParseExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class JsonParseExtension | |
{ | |
public static bool JsonTryParse<T>(this string jsonString, out T result) where T : class | |
{ | |
try | |
{ | |
result = JsonSerializer.Deserialize<T>(jsonString); | |
return true; | |
} | |
catch(Exception e) |
View ResponseFailure.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> |
View ResponseSuccess.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Code": 0, | |
"Msg": "Success!", | |
"Response": { | |
"Name": "Andy", | |
"Age": 28 | |
} | |
} |