Search code examples
c#arraysjsonresthttpclient

JSON with Array Respone from API


i have a HTTPclient and im sending request to a Server. The response from the server is a JSON. In this Json is also an array. I want do display the information from the JSON in the consol. I get all information from the JSON, but i dont get the array displayed in my console.

The JSON looks like this:

{
    "Name1": "Karl",
    "Name2": "Peter",
    "Name3": "Wilhelm",
    "PreNames": {
        "PreName": "Werner",
        "PreName2": "Josef"
    }
}

So now my Code so far:

public class PreNames //for Array
    {
        public string PreName { get; set; }
        public string PreName2{ get; set; }
    }

    public class Names//for JSON
    {
        public string Name1{ get; set; }
        public string Name2{ get; set; }
        public string Name3{ get; set; }
    }

And my Main:

if (Response().IsSuccessStatusCode)
        {
            var namesJ= Response().Content.ReadAsAsync<IEnumerable<Names>>().Result;
            var preNamesJ= await Response().Content.ReadAsAsync<List<PreNames>>();



            foreach (var a in namesJ)

            {

                Console.WriteLine("Name1: {0}", a.Name1);

                foreach(var b in preNamesJ)
                {
                    Console.WriteLine("{0}", b.PreName);
                }
                
            }

So in the console are all names displayed, but not the PreNames so i cant get the PreNames from the array in the JSON...

I Hope somebody could help me :)


Solution

  • @Leonc443, Here is a working code

        using Newtonsoft.Json;  //add this nuget package
        using System;
    
        namespace ConsoleApp
        {
            class Program
            {
                public static void Main(string[] args)
                {
                    string json = @"{
                        'Name1': 'Karl',
                        'Name2': 'Peter',
                        'Name3': 'Wilhelm',
                        'PreNames': {
                                    'PreName': 'Werner',
                                    'PreName2': 'Josef'
                                }
                    }";  // data received from api
                    var names = JsonConvert.DeserializeObject<Names>(json);
                    Console.WriteLine(names.Name1);     // Karl
                    Console.WriteLine(names.Name2);     //Peter
                    Console.WriteLine(names.Name3);     //Wilhelm
                    Console.WriteLine(names.PreNames.PreName);      //Werner
                    Console.WriteLine(names.PreNames.PreName2);     // Josef
    
                }
            }
            public class Names
            {
                public string Name1 { get; set; }
                public string Name2 { get; set; }
                public string Name3 { get; set; }
                public PreNames PreNames { get; set; }
            }
    
            public class PreNames
            {
                public string PreName { get; set; }
                public string PreName2 { get; set; }
            }
        }
    

    Please let me know if this helps