2

我需要反序列化以下内容:

{"result":{"success":true,"value":"8cb2237d0679ca88db6464eac60da96345513964"}}

使用 Newtonsoft.Json 到 C# 对象

WebClient wc = new WebClient();
var json = wc.DownloadString(url);
Worker w = JsonConvert.DeserializeObject<Worker>(json);

这是课程代码:

public class Worker
{

    [JsonProperty("success")]
    public string success { get; set; }

    [JsonProperty("value")]
    public string value { get; set; }
}

代码没有报错,但是成功值为空。

4

3 回答 3

5

您缺少外部对象。

public class Worker
{
     [JsonProperty("result")]
     public Result Result { get; set; }
}

public class Result
{
    [JsonProperty("success")]
    public string Success { get; set; }

    [JsonProperty("value")]
    public string Value { get; set; }
}
于 2013-01-28T19:56:42.427 回答
0

我不熟悉那个库,但成功和结果看起来都是对象“结果”的属性

你试过[JsonProperty("result.success")]吗?

编辑:好吧,不管它看起来像一个范围问题。查看文档后,这是我的新建议:

public class Result{
 [JsonProperty("result")]
 public Worker result { get; set; }
}

然后Json.Convert.Deserialize<Result>(json)改为。

于 2013-01-28T19:56:14.667 回答
0

您不需要任何课程,可以使用dynamic关键字

string json = @"{""result"":{""success"":true,""value"":""8cb2237d0679ca88db6464eac60da96345513964""}}";

dynamic dynObj = JsonConvert.DeserializeObject(json);
Console.WriteLine("{0} {1}", dynObj.result.success, dynObj.result.value);
于 2013-01-28T19:58:17.190 回答