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

Newtonsoft反序列化为Dictionary<string,object>后如何获取嵌套属性值

问题原因

你当前使用Dictionary<string, object>接收反序列化结果时,JSON中的嵌套对象(customer、products、order节点)不会被自动转换为嵌套字典结构,而是会被Newtonsoft.Json解析为内置的JObject类型,因此无法直接通过字典索引获取子属性。
以下是三种可直接落地的实现方案:

方案1:直接使用JObject解析(无需定义额外类,实现最简单)

不需要先转Dictionary,直接将JSON字符串解析为JObject,通过链式索引器即可访问任意层级的节点属性:

using Newtonsoft.Json.Linq;

// 解析JSON
JObject jsonData = JObject.Parse(test);

// 顶层属性取值
string status = jsonData["status"].ToString();
string testField = jsonData["test"].ToString();

// order节点取值
string orderName = jsonData["order"]["name"].ToString();
int orderQty = int.Parse(jsonData["order"]["qty"].ToString());

// customer节点取值
string customerName = jsonData["customer"]["name"].ToString();
string customerAddress = jsonData["customer"]["address"].ToString();

// products节点取值
string productName = jsonData["products"]["name"].ToString();
int productQty = int.Parse(jsonData["products"]["qty"].ToString());

如果需要兼容节点为空的场景,可以加空传播运算符避免报错:

string productName = jsonData["products"]?["name"]?.ToString();

方案2:保留Dictionary接收逻辑,二次转换嵌套节点

如果业务逻辑要求必须用Dictionary<string, object>作为反序列化的接收类型,只需要将嵌套的JObject节点二次转换为字典即可取值:

Dictionary<string, object> values = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(test);

// 提取customer节点属性
var customerJObj = values["customer"] as JObject;
Dictionary<string, object> customerDict = customerJObj.ToObject<Dictionary<string, object>>();
string customerName = customerDict["name"].ToString();
string customerAddress = customerDict["address"].ToString();

// 提取products节点属性
var productJObj = values["products"] as JObject;
Dictionary<string, object> productDict = productJObj.ToObject<Dictionary<string, object>>();
string productName = productDict["name"].ToString();
string productQty = productDict["qty"].ToString();

方案3:强类型映射(生产环境推荐,类型安全无转换错误)

定义和JSON结构完全匹配的C#类,反序列化时直接输出强类型对象,不需要手动做类型转换,可维护性更高:

// 定义和JSON结构对应的实体类
public class OrderInfo
{
    public string name { get; set; }
    public string qty { get; set; }
}
public class CustomerInfo
{
    public string name { get; set; }
    public string address { get; set; }
}
public class ProductInfo
{
    public string name { get; set; }
    public string qty { get; set; }
}
public class JsonRoot
{
    public string status { get; set; }
    public string test { get; set; }
    public OrderInfo order { get; set; }
    public CustomerInfo customer { get; set; }
    public ProductInfo products { get; set; }
}

// 反序列化为强类型对象
JsonRoot data = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonRoot>(test);

// 直接通过属性访问即可,无需额外转换
string customerName = data.customer.name;
string customerAddress = data.customer.address;
string productName = data.products.name;
string productQty = data.products.qty;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.02 07:06:34