Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.12/5 (6 votes)
See more:
Hi, How can I convert following JSON to List of Object.

[snipped hundreds of lines of JSON]

What I have tried:

C#
QPayBrandRootObject qPayBrandRootObject = JsonConvert.DeserializeObject<QPayBrandRootObject>(finalResult);


This fails
Posted
Updated 21-Jun-20 6:51am
v2

 
Share this answer
 
Search on google there are many online converters available.
but your JSON should be correct and valid.
 
Share this answer
 
v2
Visual Studio: Menu Edit --> "Paste Special" --> Paste JSON As Classes

On project with target framework 4.5 or abone
 
Share this answer
 
Hi, I am sharing the code.
You can get c# object using this example.

C#
public class User
{
    public User(string json)
    {
        JObject jObject = JObject.Parse(json);
        JToken jUser = jObject["user"];
        name = (string) jUser["name"];
        teamname = (string) jUser["teamname"];
        email = (string) jUser["email"];
        players = jUser["players"].ToArray();
    }

    public string name { get; set; }
    public string teamname { get; set; }
    public string email { get; set; }
    public Array players { get; set; }
}

// Use
private void Run()
{
    string json = @"{""user"":{""name"":""asdf"",""teamname"":""b"",""email"":""c"",""players"":[""1"",""2""]}}";
    User user = new User(json);

    Console.WriteLine("Name : " + user.name);
    Console.WriteLine("Teamname : " + user.teamname);
    Console.WriteLine("Email : " + user.email);
    Console.WriteLine("Players:");

    foreach (var player in user.players)
        Console.WriteLine(player);
 }



Hope it will help you out.
 
Share this answer
 
Create your class. Create a JavaScriptSerializer instance. Use that instance to deserialize the JSON data into a list of your class type. You can then access that list however you want, such as through a foreach, etc.

Example:
C#
// Simple class containing some properties for a vehicle.
class Vehicle
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Region { get; set; }
    public string Garage { get; set; }
    public decimal Latitude { get; set; }
    public decimal Longitude { get; set; }
    public decimal Speed { get; set; }
    public decimal Heading { get; set; }
}

// Create JavaScriptSerializer instance.
System.Web.Script.Serialization.JavaScriptSerializer jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

// Deserialize JSON data into list of Vehicles.
List<Vehicle> finalResult =  jsSerializer.Deserialize<List<Vehicle>>(jsonData);

// Loop through the final result and access properties of objects in list
foreach (Vehicle veh in finalResult)
{
    Console.WriteLine(veh.Name);
    Console.WriteLine(veh.Region);
    Console.WriteLine(veh.Garage);
    // Continue with other properties as needed...
}
 
Share this answer
 
Comments
Richard Deeming 5-Sep-17 13:15pm    
October 2016!

I suspect a spam answer was posted and then removed, which has dragged this old question back up into the active list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900