How to convert JSON Object to JSON Array in C# Restsharp (Exception- cannot convert newtonsoft.json.linq.jobject to newtonsoft.json.linq.array”)

Below is my Requirement-

Actual JSON= { “page”:2, “per_page”:6, “total”:12, “total_pages”:2 }

I want to make it as below-

[ { “page”:2, “per_page”:6, “total”:12, “total_pages”:2 } ]

It would be great help if someone can provide the best logic to do this in C# (RestSharp)

Answer

Basically you can store your json data inside the list.

var json = new
{
   name = "john doe",
   age = 20
};

var list = new List<dynamic>();

list.Add(json);

Later on, when you serialize your list. It will look like this:

[{"name":"john doe","age":20}]

Hopefully it helps.

Leave a Reply

Your email address will not be published. Required fields are marked *