Click here to Skip to main content
14,576,820 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi, How can I convert following JSON to List of Object.

[snipped hundreds of lines of JSON]

What I have tried:

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


This fails
Posted
Updated 21-Jun-20 6:51am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 3

   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Search on google there are many online converters available.
but your JSON should be correct and valid.
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

Visual Studio: Menu Edit --> "Paste Special" --> Paste JSON As Classes

On project with target framework 4.5 or abone
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

Hi, I am sharing the code.
You can get c# object using this example.

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 6

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:
// 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...
}
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100