0

I've tried different solutions but I'm not reaching anywhere.

I'm in a middle of a loop and sometimes I need to add data to an existing property (details in this case).

So, in the beginning I create the following JObject without any problem:

var json = JsonConvert.SerializeObject(
    new {
      details = new[]{ 
        new{product_name        = detail["product_name"].ToString(), 
            quantity            = detail["quantity"].ToString(),
            product_options     = detail["product_options"].ToString()},
      }
    }
);

// _elements is an dictionary<int, JObject>
_elements.Add(id, JObject.Parse(json));

// output 
{
    "details": [

        {
            "product_name": "Oranges",
            "quantity": "2",
            "product_options": [],
        }

    ]
}

But, for some reason, I need to add more products to the list of details, so I would like my output to be:

{
    "details": [

        {
            "product_name": "Oranges",
            "quantity": "2",
            "product_options": [],
        },

        {
            "product_name": "Coca Cola",
            "quantity": "5",
            "product_options": [],
        }
    ]
}

I've tried so far without any success:

dic.Value.Property("details").Add(json);
dic.Value.SelectToken("details").Add(json);

1 Answer 1

1

Solved.

dic.Value["details"].Last.AddAfterSelf(JObject.Parse(json));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.