DiMiaN
DiMiaN
CC#
Created by DiMiaN on 3/14/2023 in #help
✅ How to add variable instead of value in JSON dynamic array?
using Newtonsoft.Json;
using System;

public class ConsoleApp7
{
public static void Main()
{
string json = @"
{
""toon_guid_by_gateway"": {
""10"": {
""Cwt)dewalt"": 4,
""Dewalt1"": 6,
""PUKAN"": 14
},
""11"": {
""Dzergalt"": 8,
""Fotono4kiFTW"": 9,
""kekterrek"": 13
},
""20"": {
""BSL15-Dewalt"": 3,
""Cwt)Dewalt"": 5,
""iRk-Dewalt"": 12
},
""30"": {
""BSL-Dewalt"": 2,
""DewaltTv"": 7,
""IllIlIIIllllll1"": 11
},
""45"": {
""asdas12qsdasdas"": 1,
""gantel'kaFTW"": 10
}
}
}";
int gateway = 10;
string player = "Dewalt1";

dynamic dynamicData = JsonConvert.DeserializeObject<dynamic>(json);
Console.WriteLine(dynamicData.toon_guid_by_gateway[Convert.ToString(gateway)].PUKAN);
}
}
using Newtonsoft.Json;
using System;

public class ConsoleApp7
{
public static void Main()
{
string json = @"
{
""toon_guid_by_gateway"": {
""10"": {
""Cwt)dewalt"": 4,
""Dewalt1"": 6,
""PUKAN"": 14
},
""11"": {
""Dzergalt"": 8,
""Fotono4kiFTW"": 9,
""kekterrek"": 13
},
""20"": {
""BSL15-Dewalt"": 3,
""Cwt)Dewalt"": 5,
""iRk-Dewalt"": 12
},
""30"": {
""BSL-Dewalt"": 2,
""DewaltTv"": 7,
""IllIlIIIllllll1"": 11
},
""45"": {
""asdas12qsdasdas"": 1,
""gantel'kaFTW"": 10
}
}
}";
int gateway = 10;
string player = "Dewalt1";

dynamic dynamicData = JsonConvert.DeserializeObject<dynamic>(json);
Console.WriteLine(dynamicData.toon_guid_by_gateway[Convert.ToString(gateway)].PUKAN);
}
}
Instead of value "PUKAN" I want the data from string player instead.
3 replies
CC#
Created by DiMiaN on 3/12/2023 in #help
✅ How to add data to a list that is outside of Main()?
using System.Collections.Generic;
internal class Program
{
public static void insertData(string animal, int amount)
{
var list = new List<KeyValuePair<string, int>>();
list.Add(new KeyValuePair<string, int>(animal, amount));
}
private static void Main(string[] args)
{
insertData("Dog", 4);
insertData("Cat", 12);

foreach(var element in list)
{
Console.WriteLine(element);
}
}
}
using System.Collections.Generic;
internal class Program
{
public static void insertData(string animal, int amount)
{
var list = new List<KeyValuePair<string, int>>();
list.Add(new KeyValuePair<string, int>(animal, amount));
}
private static void Main(string[] args)
{
insertData("Dog", 4);
insertData("Cat", 12);

foreach(var element in list)
{
Console.WriteLine(element);
}
}
}
I know there are some errors but I can't figure out how to solve this. Also when calling the insertData the list will be resetted every new call?
19 replies
CC#
Created by DiMiaN on 3/11/2023 in #help
✅ Outputting values from a list
When typing in name and age it doesnt display the correct values. Instead it outputs: Program+Person.
internal class Program
{
public class Person
{
public string name { get; }
public int age { get; set; }
public int weight { get; set; }
public int height { get; set; }

public Person(string name, int age)
{
this.age = age;
this.name = name;
this.weight = 0;
this.height = 0;
}

// Rest of the Person
}

private static void Main(string[] args)
{
List<Person> persons = new List<Person>();

// Read the names of persons from the user
while (true)
{
Console.Write("Enter a name, empty will stop: ");
String name = Console.ReadLine();
if (name == "")
{
break;
}

Console.Write("Enter the age of the person " + name + ": ");

int age = Convert.ToInt32(Console.ReadLine());

// Add to the list a new person
// whose name is the previous user input
persons.Add(new Person(name, age));
}

// Print the number of the entered persons, and their individual information
Console.WriteLine();
Console.WriteLine("Persons in total: " + persons.Count);
Console.WriteLine("Persons: ");

foreach (Person person in persons)
{
Console.WriteLine(person);
}
}
}
internal class Program
{
public class Person
{
public string name { get; }
public int age { get; set; }
public int weight { get; set; }
public int height { get; set; }

public Person(string name, int age)
{
this.age = age;
this.name = name;
this.weight = 0;
this.height = 0;
}

// Rest of the Person
}

private static void Main(string[] args)
{
List<Person> persons = new List<Person>();

// Read the names of persons from the user
while (true)
{
Console.Write("Enter a name, empty will stop: ");
String name = Console.ReadLine();
if (name == "")
{
break;
}

Console.Write("Enter the age of the person " + name + ": ");

int age = Convert.ToInt32(Console.ReadLine());

// Add to the list a new person
// whose name is the previous user input
persons.Add(new Person(name, age));
}

// Print the number of the entered persons, and their individual information
Console.WriteLine();
Console.WriteLine("Persons in total: " + persons.Count);
Console.WriteLine("Persons: ");

foreach (Person person in persons)
{
Console.WriteLine(person);
}
}
}
15 replies