简体   繁体   English

Newtonsoft Json.Net-如何避免使用Json属性?

[英]Newtonsoft Json.Net - How to avoid using Json properties?

I'm using the Json.Net Newtonsoft component throughout my project at work. 我在工作的整个项目中都使用Json.Net Newtonsoft组件。

From time to time I stumble into the implementation details of the library. 我不时偶然发现该库的实现细节。 For example when I'm fetching the JProperty's value, I'm forced to use the .Value property: 例如,当我获取JProperty的值时,我被迫使用.Value属性:

var displaySettings = options.DisplaySettings.Value; var displaySettings = options.DisplaySettings.Value;

This is annoying, as one has to remember that the properties of given dynamic object can be really retrieved by "Value" property, without really knowing its type at runtime. 这很烦人,因为必须记住,给定动态对象的属性实际上可以通过“ Value”属性来检索,而不必在运行时真正知道其类型。 (And additionally, there is more linq code to Select the "Value", which clutters the code) (此外,还有更多的linq代码来选择“值”,这会使代码混乱)

Is there a way to easily wrap the Json object or maybe use the Newtonsoft component more proper way? 有没有一种方法可以轻松包装Json对象,或者使用更合适的Newtonsoft组件?


I see I'm getting down-voted, but I think there is a problem with JObject encapsulation. 我看到我的投票率下降了,但是我认为JObject封装存在问题。

If I use "var" to store the bool variable, the test would fail, because the variable will contain JValue object. 如果我使用“ var”存储bool变量,则测试将失败,因为该变量将包含JValue对象。

[Test]
public void TestProperties()
{
    dynamic testee = JsonConvert.DeserializeObject(@"
    {
        TestBool:true
    }
    ");
    var result = testee.TestBool;

    Assert.That(result, Is.EqualTo(true));
}

This test will pass: 该测试将通过:

Assert.That(result.Value, Is.EqualTo(true));

The test will result in: 该测试将导致:

Expected: True But was: 预期:是,但是:

Also, to prove that this isn't NUnit issue: 另外,要证明这不是NUnit问题:

dynamic testee = JsonConvert.DeserializeObject(@"
{
    TestBool:true
}
");

var result = testee.TestBool;

if (result)
{
    Assert.Pass();
    return;
}

The above will throw exception on "if" clause. 以上将在“ if”子句上引发异常。

I wonder if it's a common practice on stackoverflow to down-vote people without asking for clarification. 我想知道是否在不要求澄清的情况下,将堆栈溢出用于降低投票人数是一种常见的做法。 Nice culture. 优美的文化。 (I can take, that some of the people new to given technology would be discouraged to ask questions at all) (我可以认为,不鼓励使用某些新技术的人根本不提出问题)

Thanks, AD 谢谢,公元

Deserialize into C# classes. 反序列化为C#类。

class Program
{
    static void Main(string[] args)
    {
        string json = @"{ ""FirstName"":""Bob"" }";

        Person person = Newtonsoft.Json.JsonConvert.DeserializeObject<Person>(json);

        Console.WriteLine(person.FirstName);
    }

    public class Person
    {
        public string FirstName { get; set; }
    }
}

http://www.newtonsoft.com/json/help/html/deserializeobject.htm http://www.newtonsoft.com/json/help/html/deserializeobject.htm

You have many options to handle the dynamic values with Json.Net 您可以使用许多选项来使用Json.Net处理动态

dynamic jobj = JObject.FromObject(new { DisplaySettings = "aaa" });

var ds1 = jobj.DisplaySettings; //ds1 is JValue
var ds2 = jobj.DisplaySettings.Value; //ds2 is object (boxed string)
var ds3 = (string)jobj.DisplaySettings; //ds3 is string (explicit casting)
string ds4 = jobj.DisplaySettings; //ds4 is string (implicit casting)

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

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