DeserializeObject<字典< string,object> JSON字符串

发布于 2025-02-05 12:11:39 字数 511 浏览 1 评论 0原文

我有这个JSON请求,

{
  "status": "completed",
  "test":"hi",
  "order":{
      "name":"book",
      "qty":"2"
  },
  "customer":{
    "name":"sajid",
    "address":"salmiya"
  },
  "products":{
    "name":"abc",
    "qty":"1"
  }
}

我已经使用此请求了JSON

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

,但我无法获得产品名称和数量的值 我想要客户的价值。

I have this JSON request

{
  "status": "completed",
  "test":"hi",
  "order":{
      "name":"book",
      "qty":"2"
  },
  "customer":{
    "name":"sajid",
    "address":"salmiya"
  },
  "products":{
    "name":"abc",
    "qty":"1"
  }
}

I have already Deserialized JSON using this

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

But I am unable to get the values of product name and quantity
and similar way I want the values for customer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红衣飘飘貌似仙 2025-02-12 12:11:39

在您的字典中,键是字符串类型,但值为局限。要获取数据,您必须将字典价值施加到

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

Dictionary<string,string> products =((JObject) values["products"]).ToObject<Dictionary<string,string>>();

//or
var products = (JObject) values["products"];

var productName= products["name"];
var productQty = products["qty"];

// or directly

productName= (string) ((JObject) values["products"])["name"];
productQty = (string) ((JObject) values["products"])["qty"];

In your dictionay a key is a string type, but a value is JObject. To get data you have to cast a dictionary value to JObject

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

Dictionary<string,string> products =((JObject) values["products"]).ToObject<Dictionary<string,string>>();

//or
var products = (JObject) values["products"];

var productName= products["name"];
var productQty = products["qty"];

// or directly

productName= (string) ((JObject) values["products"])["name"];
productQty = (string) ((JObject) values["products"])["qty"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文