【发布时间】:2014-06-17 19:52:41
【问题描述】:
我正在寻找一种使用 newtonsoft 将多个属性映射到 JSon 字符串的不同“部分”的方法。
我目前拥有的是以下内容(根本不起作用,但也许你会更好地了解我想要完成的工作):
public class example
{
[JsonProperty("this.is.an.example")]
public string ex { get; set; }
[JsonProperty("this.is.another")]
public string ex2;
}
虽然对应的 JSon 字符串可能是这样的结构:
{
"this" :
{
"is" : {
"an" : {
"example" : "this is the first value I want to return"
}
"another" : "this is the second value"
}
}
}
我想要这样,这样我就可以轻松地反序列化其中几个 JSon 字符串,如下所示:
example y = JsonConvert.DeserializeObject<example>(x);
//where x is the JSon string shown above and
//y.ex == "this is the first value I want to return"
//y.ex2 == "this is the second value
//Also note that example is the class name of the resulting object
希望您能看到我想要完成的工作。提前感谢您的帮助,感谢您提供任何意见!
注意:
经过一番搜索,我发现了一个类似的问题,但没有得到答案。但也许它会帮助JSON.NET deserialize/serialize JsonProperty JsonObject
【问题讨论】:
标签: c# json web-services json.net deserialization