You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSON字符串转C#对象失败,求正确转换实现方法

你遇到的问题核心在于序列化库不匹配:你定义的C#类使用了Newtonsoft.Json(Json.NET)的[JsonProperty]特性,但反序列化时却用了System.Web.Script.Serialization.JavaScriptSerializer,这两个库的特性不通用,导致属性映射失败,自然无法正确解析JSON。另外你的代码里还重复定义了Welcome类,这也会引发编译错误。

下面给你两种可行的解决方案:

方案一:使用Newtonsoft.Json(推荐,你的类已经适配了它)

首先修正重复的Welcome类定义,然后用适配的反序列化方式:

1. 修正后的完整类定义

using Newtonsoft.Json;

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 FileType { 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, 
    }; 
}

2. 正确的反序列化代码

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.\"}}}}";

// 方法1:用你自己封装的FromJson方法
Welcome result = Welcome.FromJson(myJson);

// 方法2:直接调用JsonConvert.DeserializeObject(效果一致)
// Welcome result = JsonConvert.DeserializeObject<Welcome>(myJson, Converter.Settings);

// 测试访问属性
Console.WriteLine($"上传状态:{result.Response.Status.Label}");
Console.WriteLine($"文件名:{result.Response.FileName}");
Console.WriteLine($"第一张图片状态:{result.Response.Te.Images[0].Status}");

注意:如果你的项目还没安装Newtonsoft.Json,需要通过NuGet包管理器安装这个包。

方案二:继续使用JavaScriptSerializer(不推荐,需调整类)

如果一定要用System.Web.Script.Serialization.JavaScriptSerializer,你需要把[JsonProperty]替换成该库对应的[ScriptName]特性,或者让属性名称和JSON键完全一致:

using System.Web.Script.Serialization;

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

public class Response 
{ 
    [ScriptName("status")]
    public Status Status { get; set; } 
    [ScriptName("md5")]
    public string Md5 { get; set; } 
    [ScriptName("file_name")]
    public string FileName { get; set; } 
    [ScriptName("file_type")]
    public string FileType { get; set; } 
    [ScriptName("features")]
    public string[] Features { get; set; } 
    [ScriptName("te")]
    public Te Te { get; set; } 
}

// 其他类同理,将JsonProperty替换为ScriptName即可

// 反序列化代码
JavaScriptSerializer serializer = new JavaScriptSerializer();
Welcome result = serializer.Deserialize<Welcome>(myJson);

// 测试访问属性
Console.WriteLine(result.Response.Status.Label);

内容的提问来源于stack exchange,提问作者user9250176

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:39:01