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

JsonConvert.DeserializeAnonymousType定义语法问题及多维数组反序列化

解决匿名类型反序列化多维数组返回null的问题

咱先捋清楚问题根源:你原来的匿名类型定义和PHP接口返回的JSON结构不匹配!

你之前写的accountinformation = new[] { "" , "" , "" }是一维字符串数组(对应string[]),但看PHP的逻辑:循环里往$account_information中添加array( $billing_company, $customer_account_number, $customer_account_manager ),最终生成的是二维数组(数组的数组),对应的JSON结构是"account_information": [["xxx","xxx","xxx"], ["yyy","yyy","yyy"], ...]。结构不匹配,反序列化自然就返回null了。

正确的匿名类型定义语法

你只需要把accountinformation定义成二维字符串数组的结构就行,示例代码如下:

// 匹配PHP返回的二维数组结构的匿名类型
var definition = new { 
    result = "", 
    accountinformation = new[] { new[] { "", "", "" } } 
};
var accountInformationResult = JsonConvert.DeserializeAnonymousType(responseBody, definition);

如果想更直观,也可以显式指定类型(效果完全一致):

var definition = new { 
    result = "", 
    accountinformation = new string[][] { new string[3] } 
};

这样定义后,Json.NET就能正确识别二维数组的结构,把接口返回的内容反序列化到匿名类型里了,不用折腾dynamic~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 06:31:05