gpt4 book ai didi

c# - 将 Json 字符串转换为 C# 对象无效

转载 作者:太空宇宙 更新时间:2023-11-03 22:48:52 24 4
gpt4 key购买 nike

我想将此 Json 字符串转换为 C# 对象,我尝试从一些教程中学习但没有用,我正在尝试转换此 json 字符串:

 string myJson = "{\"response\": {\"status\": {\"code\": 1002,\"label\": \"UPLOAD_SUCCESS\",\"message\": \"The file was uploaded successfully.\"},\"md5\": \"da855ff838250a05692f14e\",\"file_name\": \"MyFile.docx\",\"file_type\": \"docx\",\"features\": [ \"te\" ],\"te\": {\"images\": [{\"report\": {\"verdict\": \"unknown\"},\"status\": \"not_found\",\"id\": \"5e5de275-a103-4f67-b532918fa59\",\"revision\": 1},{\"report\": {\"verdict\": \"unknown\"},\"status\": \"not_found\",\"id\": \"7e6fe36e-889e-4c25-8704-5637830df\",\"revision\": 1}],\"status\": {\"code\": 1001,\"label\": \"FOUND\",\"message\": \"The requested data has been found.\"}}}}";

我创建了这个类:

 public partial class Welcome
{
[JsonProperty("response")]
public Response response { get; set; }
}

public partial class Response
{
[JsonProperty("status")]
public Status status { get; set; }

[JsonProperty("md5")]
public string md5 { get; set; }

[JsonProperty("file_name")]
public string fileName { get; set; }

[JsonProperty("file_type")]
public string file_type { get; set; }

[JsonProperty("features")]
public string[] features { get; set; }

[JsonProperty("te")]
public Te te { get; set; }
}

public partial class Status
{
[JsonProperty("code")]
public long code { get; set; }

[JsonProperty("label")]
public string label { get; set; }

[JsonProperty("message")]
public string message { get; set; }
}

public partial class Te
{
[JsonProperty("images")]
public Image[] images { get; set; }

[JsonProperty("status")]
public Status status { get; set; }
}

public partial class Image
{
[JsonProperty("report")]
public Report report { get; set; }

[JsonProperty("status")]
public string status { get; set; }

[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("revision")]
public long Revision { get; set; }
}

public partial class Report
{
[JsonProperty("verdict")]
public string verdict { get; set; }
}

public partial class Welcome
{
public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, Converter.Settings);
}

public static class Serialize
{
public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, Converter.Settings);
}

public class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
};
}

最后我尝试访问对象的值:

    Welcome result = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Welcome>(json);
Console.WriteLine(result);

你能告诉我如何将这个 Json 字符串转换为 C# 对象吗?我找了很多教程,但有一段时间没有成功,所以也许你可以帮我节省一些时间。

最佳答案

你应该使用

Welcome result = JsonConvert.DeserializeObject<Welcome>(myJson);

然后你可以很容易地通过使用访问它

Console.WriteLine(result.response.status.code);

关于c# - 将 Json 字符串转换为 C# 对象无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48545156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com