web API 错误处理The 'ObjectContent`1' type failed to serialize

本文介绍如何通过修改 WebApiConfig.cs 文件中的配置将返回格式从 XML 转换为 JSON。具体操作包括设置 Newtonsoft.Json 库以保持对象引用,并移除 XML 格式的输出。

just added this three lines in my WebApiConfig.cs file...



var json = config.Formatters.JsonFormatter;

json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;

config.Formatters.Remove(config.Formatters.XmlFormatter);



After this codes were added.the Return XML will be transferred to Json format

Function CallDeepSeekAPI(api_key As String, inputText As String) As String Dim API As String Dim SendExt As String Dim Http As Object Dim status_code As Integer Dim response As String API = "http://api.siliconflow.cn/v1/chat/completions" SendExt = "{""model"": ""deepseek-ai/DeepSeek-V3"", ""messages"": [{""role"":""system"",""content"":""You are a helpful assistant.""},{""role"":""user"",""content"":""" & inputText & """}]}" On Error Resume Next Set Http = CreateObject("MSXML2.XMLHTTP.6.0") If Err.Number <> 0 Then CallDeepSeekAPI = "Error: Failed to create HTTP object - " & Err.Description Exit Function End If With Http .Open "POST", API, False .setRequestHeader "Content-Type", "application/json; charset=utf-8" .setRequestHeader "Authorization", "Bearer " & api_key .Send SendExt If Err.Number <> 0 Then CallDeepSeekAPI = "Error: API request failed - " & Err.Description Exit Function End If status_code = .Status response = .responseText End With On Error GoTo 0 If status_code = 200 Then CallDeepSeekAPI = response Else CallDeepSeekAPI = "HTTP " & status_code & " : " & response End If Set Http = Nothing End Function Sub DeepSeekV3() Dim api_key As String Dim inputText As String Dim response As String Dim regex As Object Dim matches As Object Dim originalSelection As Range ' 设置 API 密钥 api_key = "sk-wppmbbubdtfxubssdrvbausirpllaxgivsaglqhomkasrvzd" ' 检查 API 密钥是否为空 If api_key = "" Then MsgBox "请填写API密钥", vbCritical Exit Sub End If ' 检查是否选择了文本 If Selection.Type <> wdSelectionNormal Then MsgBox "请先选择需要处理的文本", vbExclamation Exit Sub End If ' 保存当前选中的文本范围 Set originalSelection = Selection.Range inputText = Trim(Replace(Selection.Text, vbCr, "")) ' 调用 DeepSeek API response = CallDeepSeekAPI(api_key, inputText) ' 检查 API 调用是否出错 If Left(response, 5) = "Error" Then MsgBox response, vbCritical Exit Sub End If ' 使用正则表达式解析 API 响应 Set regex = CreateObject("VBScript.RegExp") With regex .Global = True .MultiLine = True .IgnoreCase = False .Pattern = """content""\s*:\s*""([\s\S]*?)""" End With If matches.Count > 0 Then ' 提取并处理响应内容 response = matches(0).SubMatches(0) ' 将 \n\n 替换为段落标记 replacedText = Replace(response, "\n\n", vbCrLf) ' 将处理后的内容插入文档 originalSelection.Collapse Direction:=wdCollapseEnd originalSelection.InsertAfter vbNewLine & replacedText originalSelection.Start = originalSelection.Start + Len(vbNewLine & replacedText) Else MsgBox "API响应解析失败:" & vbNewLine & replacedText, vbExclamation End If End Sub 帮我看看这段vb代码有哪些错误 帮我修改一下
最新发布
09-19
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值