JObject nested property

前端 未结 2 1326
一整个雨季
一整个雨季 2021-02-13 20:11

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

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

I can add

相关标签:
2条回答
  • 2021-02-13 20:46

    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" }
            }
        }
    };
    
    0 讨论(0)
  • 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:

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

    0 讨论(0)
提交回复
热议问题