How to add jarray Object into JObject

前端 未结 4 1599
旧巷少年郎
旧巷少年郎 2021-02-05 03:07

How to add JArray into JObject? I am getting an exception when changing the jarrayObj into JObject.

paramete         


        
4条回答
  •  情书的邮戳
    2021-02-05 03:36

    // array of animals
    var animals = new[] { "cat", "dog", "monkey" };
    
    // our profile object
    var jProfile = new JObject
            {
                { "birthday", "2011-05-06" },
                { "email", "dude@test.com" }
            };
    
    // Add the animals to the profile JObject
    jProfile.Add("animals", JArray.FromObject(animals));
    
    Console.Write(jProfile.ToString());
    

    Outputs:

    {    
      "birthday": "2011-05-06",
      "email": "dude@test.com",
      "animals": [
        "cat",
        "dog",
        "monkey"
      ]
    }
    

提交回复
热议问题