1 year ago

#375035

test-img

Ankur

Add JsonObject to JsonArray

I am using System.Text.Json

My sample json is

{
    "a": {}
}

I am reading key "a" from json and converting JsonNode to JsonObject.

JsonObject jsonObject = jsonForTest.AsObject();
JsonArray array = new JsonArray();                
array.Add(jsonForTest);

When I do array.add() It gives an exception

System.InvalidOperationException: The node already has a parent.

--- Stack Trace: 
ThrowHelper.ThrowInvalidOperationException_NodeAlreadyHasParent()
JsonNode.AssignParent(JsonNode parent)
JsonArray.Add(JsonNode item)

I have tried to add the object to array but it doesn't work. It should work as in Newtonsoft.Json to add the object to array.

c#

json

.net

json.net

system.text.json

0 Answers

Your Answer

Accepted video resources