JeffreySu/WeiXinMPSDK · error · JsonException
Expected a string or integer JSON value.
Error message
Expected a string or integer JSON value.
What it means
A custom System.Text.Json converter in MeetingPhoneJson reads a JSON value that must be either a string or an integer and converts it to string. If the token is any other type (bool, object, array, null), Read throws JsonException("Expected a string or integer JSON value.").
Solutions
- Inspect the raw JSON at that field and confirm it is a string or number; fix the payload source or the test fixture
- Map the field to a raw JsonElement/object property and normalize manually if the API is inconsistent
- Use JsonElement.Type checks or a more lenient converter that coerces bool/null to string
- Pin/check SDK version against the current WeCom Meeting API response schema
Example fix
// before
public string Phone { get; set; } // converter throws on bool/null
// after
public JsonElement PhoneRaw { get; set; }
[JsonIgnore] public string Phone => PhoneRaw.ValueKind == JsonValueKind.String ? PhoneRaw.GetString() : PhoneRaw.ToString(); Defensive patterns
Strategy: try-catch
Validate before calling
if (token is JsonElement je && je.ValueKind is not (JsonValueKind.String or JsonValueKind.Number)) throw new JsonException($"Unexpected kind {je.ValueKind}"); Type guard
static bool IsStringOrNumber(JsonValueKind kind) => kind is JsonValueKind.String or JsonValueKind.Number;
Try / catch
try { var dto = JsonSerializer.Deserialize<MeetingPhoneResponse>(json); } catch (JsonException ex) { log.Error(ex, "Unexpected token type in meeting-phone payload"); } Prevention
- Log raw payloads on JsonException to spot API schema drift
- Keep test fixtures type-accurate to the real API
- Use JsonElement for fields the API sends inconsistently
When it happens
Trigger: Deserializing meeting/phone JSON where a normally string-or-number field arrives as bool/object/array/null — e.g. the API changed shape, or a caller supplies handcrafted JSON with an unexpected type for that field.
Common situations: WeCom returning error payloads or a new field type inside a meeting-phone response; test fixtures written with wrong types; deserializing with the wrong target model so the converter is applied to an unrelated token.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- 无法解析 Finance SDK 返回的会话内容 JSON。
- 专区程序事件 JSON 不能为空。
- JSON 反序列化结果为空,目标类型:
- GetInstance 失败,此类型无法自动生成:
- ArgumentOutOfRangeException (msgType)
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/d08eb96b28280837.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.Work/Senparc.Weixin.Work/AdvancedAPIs/Meeting/MeetingPhoneJson.cs:190
public IList<MeetingPhoneTempOpenIdItem> tmp_openid_list { get; set; }
}
internal sealed class MeetingStringOrNumberJsonConverter : JsonConverter<string>
{
public override string Read(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
return reader.GetString();
}
if (reader.TokenType == JsonTokenType.Number && reader.TryGetInt64(out var integer))
{
return integer.ToString(CultureInfo.InvariantCulture);
}
throw new JsonException("Expected a string or integer JSON value.");
}
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
=> writer.WriteStringValue(value);
}
}
View on GitHub (pinned to be573f6f94)