0

I'm new in asp.net, I have a problem with structs

This is my struct:

public struct Attribute
{
    public const string Name = "name";
    public const string Desc= "short_description";
    
    public const string Width= "description";
    public const string Height= "package_length";
}

This is my code :

 var jsonValue = JsonConvert.SerializeObject(ValueFromAPI);

Dictionary<string, object> dictValues = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonValue );


foreach (var dictValue in dictValues )
{
    if(dictValue.Key == Attribute.Name)
    {
        //mycode
    }
    else if(dictValue.Key == Attribute.Desc)
    {
        //mycode
    }
    else if(dictValue.Key == Attribute.Width)
    {
            //mycode
    }
    else if(dictValue.Key == Attribute.Heigth)
    {
        //mycode
    }
}

how to check Attribute properties is contain jsonValue Key without using if else because I have a lot of attribute properties, for now, I check one by one using if else

Thanks!

adian
  • 128
  • 1
  • 14
  • I've removed the ASP.NET tag because this question doesn't seem unique to ASP.NET (there is nothing specifically related to ASP.NET in your question). – ProgrammingLlama Jul 04 '22 at 15:21
  • @DiplomacyNotWar ok thanks, do you have solution for my case ? – adian Jul 04 '22 at 15:22
  • 1
    No, because I don't really understand what you're doing. It would really help clarify the goal if you showed your existing if/else code. We could put your string values into a `List` and loop through, but if your code isn't just `if (dictValue.ContainsKey(Attribute.Name)) { Console.WriteLine("Found {0}", Attribute.Name); }` which could easily be rewritten as a loop, we'll need to understand more about the goal. – ProgrammingLlama Jul 04 '22 at 15:22
  • You should provide more information. What json are you deserializing? What are you expecting? What are you getting and how is it different? It looks like your code *should* deserialize to a `dictionary` of `string`, `object`. Is that not what you want? What does your `struct` have to do with anything? You're not using it? I suspect you actually want to create a *`Class`* to deserialize to, but it's hard to help without more info. – Jonathan Jul 04 '22 at 15:30
  • That's exactly what I expected you were doing, without the part that clarifies what `//mycode` should do, which is ultimately what I was asking for. – ProgrammingLlama Jul 04 '22 at 15:31
  • i was edited my question, so i don't wont using if else becouse i have lot of properties in struct, is is posible ? – adian Jul 04 '22 at 15:32
  • `if (new [] { "name", "short_description", "description", "package_length" }.Contains(dictValue.Key)) { //mycode }` <-- I imagine this won't suffice? – ProgrammingLlama Jul 04 '22 at 15:34
  • @DiplomacyNotWar i just want get the value from dictValue – adian Jul 04 '22 at 15:34
  • You posted your code after my comment. Are you doing anything meaningful in the `//mycode` sections? ie more than just assigning the values to something? If you're assigning values, you should create a class and deserialize to *that*. If you're doing something more, there are options. Are you sure each `Dicionary` key is present? Just call the value by the `key`. Not sure, try a `switch on`. – Jonathan Jul 04 '22 at 15:35
  • So `object value = dictValues.FirstOrDefault(dv => new [] { "name", "short_description", "description", "package_length" }.Contains(dv.Key)).Value` ? – ProgrammingLlama Jul 04 '22 at 15:37
  • 1
    More an more I'm thinking you just want to deserialize your `json` into a class. `struct` isn't the way to do that. See: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-6-0 – Jonathan Jul 04 '22 at 15:37
  • When I ask for your goal, I don't want you to reiterate that you want a different approach to the if/else statement. My goal in putting fuel in my car isn't that I want to have a car with fuel in it. My goal with driving my car isn't that I want to go for a drive. My goal is that I want to go from A to B. The constraint of your question is that you want to get a specific value from the dictionary based on a key, for each of the possible keys you have. You can either use `if (dictValues.TryGetValue(key, out var value))` or the loop as you have it. There might be a better way overall to – ProgrammingLlama Jul 04 '22 at 15:39
  • do what you are actually trying to do, but you aren't telling us what that is. – ProgrammingLlama Jul 04 '22 at 15:39
  • i wont get the value where the key is not include in the properties, – adian Jul 04 '22 at 15:39
  • Are you saying "won't" (will not) or "want"? – ProgrammingLlama Jul 04 '22 at 15:40
  • 1
    sorry for my english, i mean want – adian Jul 04 '22 at 15:41
  • Are you just trying to deserialize a changing/unknown key? – ProgrammingLlama Jul 04 '22 at 15:43
  • @DiplomacyNotWar yes – adian Jul 04 '22 at 15:44
  • https://stackoverflow.com/questions/15253875/deserialize-json-with-known-and-unknown-fields ? Also: [`JsonExtensionData`](https://www.newtonsoft.com/json/help/html/DeserializeExtensionData.htm) – ProgrammingLlama Jul 04 '22 at 15:44
  • Are you concerned about misspelling of `Height` as `Heigth`? – Wyck Jul 04 '22 at 16:01
  • @DiplomacyNotWar i think is not what i want, because i just want get the value from json wher the key is alway diferent and i don't know how much unknow key, – adian Jul 04 '22 at 16:05
  • That would read all unknown keys into a dictionary, and all known keys into their corresponding properties. It sounds like exactly what you want. – ProgrammingLlama Jul 04 '22 at 16:07
  • @DiplomacyNotWar so how can i get the key to?, because is need it – adian Jul 04 '22 at 16:11
  • The dictionary's keys collection. – ProgrammingLlama Jul 04 '22 at 16:23

1 Answers1

0

The best way to write this logic would be:

var jsonValue = JsonConvert.SerializeObject(ValueFromAPI);
Dictionary<string, object> dictValues = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonValue );
foreach (var dictValue in dictValues.Keys )
{
  switch(dictValue){
     case Attribute.Name:
     //business logic
     break;
     case Attribute.Desc:
     //business logic
     break;
     case Attribute.Width:
     //business logic  
     break;
     case Attribute.Height:
     //business logic
     break;
  }
}
Niraj Bihani
  • 124
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 05 '22 at 01:13