本文介绍了JObject的嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用JObject制作一个像这样的json对象:

I am trying to make a json object like this with JObject:

{
    "input": {
        "webpage/url": "http://google.com/"
    }
}

我可以添加以下属性:

JObject job = new JObject(
                new JProperty("website/url", "http://www.google.com") );

但是任何时候我尝试将一个对象嵌套在另一个对象中,以便我可以让其父级输入"抛出异常.

But any time I try to nest an object inside another object so I can have the parent "input" it throws an exception.

如何使用JObject嵌套属性?

How do you make nested properties with JObject?

推荐答案

最直接的方法可能是:

var input = new JObject();

input.Add("webpage/url", "http://google.com");

var obj = new JObject();

obj.Add("input", input);

哪个给您:

{
  "input": {
    "webpage/url": "http://google.com"
  }
}

另一种方式是:

var input = new JObject
{
    { "webpage/url", "http://google.com" }
};

var obj = new JObject
{
    { "input", input }
};

...或者如果您想在一个语句中全部使用它:

... Or if you wanted it all in one statement:

var obj = new JObject
{
    {
        "input",
        new JObject
        {
            { "webpage/url", "http://google.com" }
        }
    }
};

这篇关于JObject的嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 09:15