0
public class RootObject
{

[JsonProperty("585709" )]
public List<SummonerId> SummonerId { get; set; }

}

I have a class that uses the JsonProperty["585709"] for my JSON Api call. This is exactly what I want but.. the problem I am facing is , The property has to change according to the User's Id number.

What I've tried :

This seems to work however when the Json call is made the value searches for the original value "585709".

       public class CustomDataContractResolver : DefaultContractResolver
        {
        public static readonly CustomDataContractResolver Instance = new CustomDataContractResolver();

        protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);
            if (property.DeclaringType == typeof(RootObject))
            {
                if (property.PropertyName.Equals("585709", StringComparison.OrdinalIgnoreCase))
                {
                    property.PropertyName = "569099";
                }
            }
            return property;
        }
     }


  var prop = JsonConvert.SerializeObject(r, new JsonSerializerSettings {     ContractResolver = CustomDataContractResolver.Instance });
        var root = JsonConvert.DeserializeObject<RootObject>(request)

Is there a way I can change the value of JsonProperty for the Class?

halfer
  • 19,824
  • 17
  • 99
  • 186
김승진
  • 25
  • 5
  • Deserialize to a `Dictionary>` or `Dictionary>`. See [Create a strongly typed c# object from json object with ID as the name](https://stackoverflow.com/questions/34213566). – dbc Apr 28 '16 at 06:36
  • var dictionary = JsonConvert.DeserializeObject>(request); what is the correct syntax?? – 김승진 Apr 28 '16 at 06:49
  • It's `var dictionary = JsonConvert.DeserializeObject>>(request);` – dbc Apr 28 '16 at 07:18
  • how can I use the values in dictionary? – 김승진 Apr 28 '16 at 07:23
  • You can loop through them as shown here: https://msdn.microsoft.com/en-us/library/ekcfxy3x.aspx. See also https://msdn.microsoft.com/en-us/library/xfhwa508.aspx. The user ids will be deserialized as the dictionary keys. – dbc Apr 28 '16 at 07:32
  • 1
    Thank you dbc, You have helped me alot and solved the issues I was facing. Thank you again! – 김승진 Apr 28 '16 at 10:56

0 Answers0