【问题标题】:How can I get the value of the only field of a field in a json with json.net in a C# class如何在 C# 类中使用 json.net 获取 json 中字段的唯一字段的值
【发布时间】:2022-08-02 20:07:13
【问题描述】:

我想要这个 json :

{
  \"foo \": {
    \"bar\": 5
  }
}

要在此类中反序列化:

class MyClass
{
 int foo;
}

像这样 :

void MyFunction(string _JSON)
{
    string json = _JSON;
    //json == {\"foo \": {\"bar\": 5}}
    MyClass c = JsonConvert.Deserialized<MyClass>(json);
    //c.foo == 5
}
  • 为什么不直接将类结构与 JSON 结构相匹配?

标签: c# json.net


【解决方案1】:

正如@DavidG 提到的,匹配类结构,您可以轻松获得所需的值。你可以使用这个网站来帮助你:https://json2csharp.com

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    相关资源
    最近更新 更多