简体   繁体   English

JObject嵌套属性

[英]JObject nested property

I am trying to make a json object like this with JObject: 我试图用JObject创建这样的json对象:

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

I can add properties like: 我可以添加以下属性:

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. 但是每次我尝试将一个对象嵌套在另一个对象中,这样我就可以让父“输入”它抛出一个异常。

How do you make nested properties with JObject? 如何使用JObject创建嵌套属性?

Probably the most straightforward way would be: 可能最直接的方式是:

var input = new JObject();

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

var obj = new JObject();

obj.Add("input", input);

Which gives you: 哪个给你:

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

Another way would be: 另一种方式是:

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" }
        }
    }
};

Just carry on as you are, and nest them in another level: 只要继续,然后将它们嵌套在另一个层次:

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

JObject parent = new JObject(new JProperty("input", job));

parent.ToString() now gives: parent.ToString()现在给出:

{ "input": { "website/url": " http://www.google.com " } } {“input”:{“website / url”:“ http://www.google.com ”}}

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

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