Prajwal MR
Prajwal MR

Reputation: 1

Map fields dynamically in c#.Need an alternative to Reflection

I have below fields in my class

 [JsonProperty("LTR-AMT-1")]
 public string? LTRAMT1 { get; set; }
 [JsonProperty("LTR-AMT-2")]
 public string? LTRAMT2 { get; set; }
 [JsonProperty("LTR-AMT-3")]
 public string? LTRAMT3 { get; set; }
 [JsonProperty("LTR-AMT-4")]
 public string? LTRAMT4 { get; set; }
 [JsonProperty("LTR-AMT-5")]
 public string? LTRAMT5 { get; set; }
 [JsonProperty("LTR-AMT-6")]
 public string? LTRAMT6 { get; set; }
 [JsonProperty("LTR-AMT-7")]
 public string? LTRAMT7 { get; set; }
 [JsonProperty("LTR-AMT-8")]
 public string? LTRAMT8 { get; set; }
 [JsonProperty("LTR-AMT-9")]
 public string? LTRAMT9 { get; set; }
 [JsonProperty("LTR-AMT-10")]
 public string? LTRAMT10 { get; set; }
 [JsonProperty("LTR-AMT-11")]
 public string? LTRAMT11 { get; set; }
 [JsonProperty("LTR-AMT-12")]
 public string? LTRAMT12 { get; set; }
 [JsonProperty("LTR-AMT-13")]
 public string? LTRAMT13 { get; set; }
 [JsonProperty("LTR-AMT-14")]
 public string? LTRAMT14 { get; set; }
 [JsonProperty("LTR-AMT-15")]
 public string? LTRAMT15 { get; set; }
 [JsonProperty("LTR-AMT-16")]
 public string? LTRAMT16 { get; set; }
 [JsonProperty("LTR-AMT-17")]
 public string? LTRAMT17 { get; set; }
 [JsonProperty("LTR-AMT-18")]
 public string? LTRAMT18 { get; set; }
 [JsonProperty("LTR-AMT-19")]
 public string? LTRAMT19 { get; set; }
 [JsonProperty("LTR-AMT-20")]
 public string? LTRAMT20 { get; set; }
//using dictionary
//public Dictionary<string, string> LTRAMT { get; set; }

i dont get these fields in order in request body.based on position i have to map it respective LTRAMT fields i have below method to do that

 public static string LtrAmounts(List<Amounts> amounts, int i)
 {
     var filter = amounts.FirstOrDefault(x => x.position == i);
     return filter != null ? filter.value.ToString() : "";
 }

Request Body:

"amounts": [
                    {
                        "value": 0,
                        "position": 1
                    },
                    {
                        "value": 1624.96,
                        "position": 2
                    },
                    {
                        "value": 9901010,
                        "position": 4
                    },
                    {
                        "value": 35776,
                        "position": 20
                    }
                ]

I tried using reflection it is working fine but response time is more.

 for (int i = 1; i <= 20; i++)
 {
     ltr.GetType().GetProperty($"LTRAMT{i}").SetValue(ltr, amounts != null ? LtrAmounts(amounts, i) : "");
 }

Also tried dictionary

  private static Dictionary<string, string> AssignKeyAndValues(List<Amounts> amounts)
  {
      Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();

     for (int i = 1; i <= 20; i++)
     {
          string key = "LTRAMT" + i;
          string value = LtrAmounts(amounts, i);
          keyValuePairs[key] = value;
      }

      return keyValuePairs;
  }

But in the response i am getting parent object under that i have fields which is not my requirement

{
"LTRAMT":
{
"LTRAMT1":"0",
"LTRAMT2":"1624.96",
"LTRAMT3:"",
"LTRAMT4":"9901010"
.........
"LTRAMT20":"35776"

}
}

I don't want to sent parent LTRAMT in response. Need help to complete this requirement.Any other suggestion will also be helpful Thanks in Advance.

Upvotes: 0

Views: 73

Answers (1)

PinBack
PinBack

Reputation: 2574

If you use Newtonsoft, you can use the JsonExtensionData attribute. See: JsonExtensionDataAttribute

The JsonExtensionDataAttribute instructs the JsonSerializer to deserialize properties with no matching field or property on the type into the specified collection. During serialization the values in this collection are written back to the instance's JSON object.

For example:

public class Tram
{
    [JsonExtensionData]
    public Dictionary<string, object> LTRAMT { get; set; }

    [JsonProperty("OTHER")]
    public string Other { get; set; }
}

Note: You must use a Dictionary<string, object> not Dictionary<string, string>

Upvotes: 0

Related Questions