JObject nested property

前端 未结 2 1325
一整个雨季
一整个雨季 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" }
            }
        }
    };
    

提交回复
热议问题