繁体   English   中英

Newtonsoft.Json 如何反序列化 json Key : Value to C# Property

[英]Newtonsoft.Json how to deserialize json Key : Value to C# Property

我的 json 文件喜欢波纹管

{
    "Tag": "en",
    "Errors": {
        "MethodRefreshIsNull": "refreshMethod is null."
    } 
}

我想转换

"MethodRefreshIsNull": "refreshMethod is null."

public KeyInfo MethodRefreshIsNull { get; set; }

和类 KeyInfo 是

public class KeyInfo 
{
    public string Value { get; set; }

    *public void OutputToConsole()
    {
         console.WriteLine(Value);
    }*
}

那么字符串“refreshMethod 为空”。 将是Value

有什么简单的方法可以转换吗?

- - - - - - - - 更新

public void OutputToConsole()
{
     console.WriteLine(Value);
}

KeyInfo 类具有方法和其他属性。

任何时候你看到{ } ,都意味着它是一个对象。 对象由 c# 类表示。 您可以使用以下模型反序列化您的 json。

public class RootObject 
{
  public string Tag {get; set;}
  public KeyValueObject Errors {get;set;}
}

public class KeyValueObject 
{
  public string MethodRefreshIsNull {get;set;}
}

//then, you would deserialize like this,

RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonStringContent);
Console.WriteLine($"Printing Tag: {obj.Tag}");
Console.WriteLine($"Printing Error: {obj.Errors.MethodRefreshIsNull}");

// prints
en
refreshMethod is null

“refreshMethod is null”是MethodRefreshIsNull的值,它是Errors一个属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM