简体   繁体   中英

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)

Create an empty array and then push json object into that array.

Example:

var obj = { "name":"test", "email":"test@test.com" };
var list = [];
list.push(obj);

Result:

list = [{ "name":"test", "email":"test@test.com" }];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM