2

This is my json data

{
  "profiles": {
    "578ed5c5bab24186a6b0f7cd9a952e10": {
      "name": "as",
      "type": "custom",
      "created": "2022-11-02T09:56:53+03:00",
      "lastUsed": "2022-11-02T09:56:53+03:00"
    },
    "41f62754d535457db1c353872a8aafcf": {
      "name": "(Default)",
      "type": "custom",
      "created": "2022-11-02T09:56:50+03:00",
      "lastUsed": "2022-11-02T09:56:50+03:00"
    }
  },
  "selectedProfile": "578ed5c5bab24186a6b0f7cd9a952e10",
  "settings": {
    "enableAdvanced": false,
    "profileSorting": "byName"
  },
  "version": 3
}

I want to get random numberized 578ed5c5bab24186a6b0f7cd9a952e10 profile names. Everytime will be updated this data. That's why, I want to filter to profile names.

I want to get profile names values in profiles.

In normally I can access datas with this way

public class _41f62754d535457db1c353872a8aafcf
{
    public string name { get; set; }
    public string type { get; set; }
    public DateTime created { get; set; }
    public DateTime lastUsed { get; set; }
}

public class _578ed5c5bab24186a6b0f7cd9a952e10
{
    public string name { get; set; }
    public string gameDir { get; set; }
    public string type { get; set; }
    public DateTime created { get; set; }
    public DateTime lastUsed { get; set; }
}

public class Profiles
{
    public _578ed5c5bab24186a6b0f7cd9a952e10 _578ed5c5bab24186a6b0f7cd9a952e10 { get; set; }
    public _41f62754d535457db1c353872a8aafcf _41f62754d535457db1c353872a8aafcf { get; set; }
}

public class Root
{
    public Profiles profiles { get; set; }
    public string selectedProfile { get; set; }
    public Settings settings { get; set; }
    public int version { get; set; }
}

public class Settings
{
    public bool enableAdvanced { get; set; }
    public string profileSorting { get; set; }
}

I want to get without calling class Profiles,class _578ed5c5bab24186a6b0f7cd9a952e10 and public _41f62754d535457db1c353872a8aafcf

I want to get with like public {0} profiles { get; set; }//after profile firt code class

zouFrax
  • 21
  • 2
  • Try following : int length = 32; Random rand = new Random(); var results = string.Join("", Enumerable.Range(0, length).Select(x => rand.Next(0, 15).ToString("x1"))); – jdweng Nov 02 '22 at 17:32
  • 3
    What exactly is your problem? Is your problem that you don't know how to deserialize the JSON shown? If so, assuming you are trying to deserialize to some c# data model, for `profiles` use a `public Dictionary profiles { get; set; }` as shown in [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182) or [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/a/34213724/3744182). – dbc Nov 02 '22 at 17:37
  • Use a dictionary with a string key – Jonathan Alfaro Nov 02 '22 at 17:52
  • I mean I want to get profile-names without calling. I can do with write directly. – zouFrax Nov 02 '22 at 17:53
  • 1
    It is even less clear what you want based on "I want to get profile-names without calling" comment. Please try to think of another way to wording your requirement and [edit] the post to clarify. – Alexei Levenkov Nov 02 '22 at 17:58

1 Answers1

-1

you can use a Dictionary


public class Root
{
    public Dictionary<string,Profile> profiles { get; set; }
    public string selectedProfile { get; set; }
    public Settings settings { get; set; }
    public int version { get; set; }
}

public class Profile
{
    public string name { get; set; }
    public string gameDir { get; set; }
    public string type { get; set; }
    public DateTime created { get; set; }
    public DateTime lastUsed { get; set; }
}

or you can create a list of Profile as a profiles

    var jsonObject = JObject.Parse(json);
    Data data = jsonObject.ToObject<Data>();
    data.profiles = ((JObject)jsonObject["profiles"]).Properties().Select(d => ConvertToProfile(d)).ToList();

public Profile ConvertToProfile(JProperty jProp)
{
    var profile = jProp.Value.ToObject<Profile>();
    profile.Id = jProp.Name;
    return profile;
}

public class Data
{
    [JsonIgnore]
    public List<Profile> profiles { get; set; }
    public string selectedProfile { get; set; }
    public Settings settings { get; set; }
    public int version { get; set; }
}
public class Profile
{
    public string Id { get; set; }
    public string name { get; set; }
    public string gameDir { get; set; }
    public string type { get; set; }
    public DateTime created { get; set; }
    public DateTime lastUsed { get; set; }
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Duplicates to for this answer are listed in the comments... – Heretic Monkey Nov 02 '22 at 20:46
  • @HereticMonkey Sorry, I am too lazy to read comments. And IMHO the most people are like me, they prefer to see an answer, than to guess what comment is a correct answer. And I don't see a list in comments for example – Serge Nov 02 '22 at 20:49
  • I Used your code and I get this print `ConsoleApp3.Program+Profile` then I edited the this code `public class Root` ` public Dictionary>> profiles { get; set; }` ` public string selectedProfile { get; set; }` ` public Settings settings { get; set; }` ` public int version { get; set; }` Now I get this exception `Error converting value "rf" to type` `System.Collections.Generic.List1[ConsoleApp3.Program+Profile]'. Path` `profiles.ca6957ea93fd465785d9580794f50d0b.name', line 4, position 18.` – zouFrax Nov 03 '22 at 10:37
  • @zouFrax I don't have public Dictionary>> in my code. Where did you get it? My code is public Dictionary profiles { get; set; } – Serge Nov 03 '22 at 10:45
  • I mean ,I used this as your code `Dictionary profiles { get; set; }` and my print was `System.Collections.Generic.Dictionary2[System.String,ConsoleApp3.Program+Profile]` then I edited. to `public Dictionary>> profiles { get; set; }` – zouFrax Nov 03 '22 at 10:57
  • @zouFrax I am sorry but I don't know what "print" means and why you need it. This code was tested as well as all my code before publishing, and everything is working properly. – Serge Nov 03 '22 at 11:00
  • how can I contact with you – zouFrax Nov 03 '22 at 11:02
  • I sent mail @Serge – zouFrax Nov 03 '22 at 11:47