1

I have a sample json like this :

{"'1234xxxxxx'":[{"AttributeId":"1","AttributeName":"Brand","AttributeValue":""},{"AttributeId":"2","AttributeName":"Color","AttributeValue":"Red4"},{"AttributeId":"3","AttributeName":"Size","AttributeValue":"44"},{"AttributeId":"4","AttributeName":"Resolution","AttributeValue":"Full HD"}]}

I have created a sample DataContract class like this :

  [System.Runtime.Serialization.DataContract]
public  class Rootobject
{
    [System.Runtime.Serialization.DataMember]
    public attr[] attrs { get; set; }
}

[System.Runtime.Serialization.DataContract]
public  class attr
{
    [System.Runtime.Serialization.DataMember]
    public string AttributeId { get; set; }

    [System.Runtime.Serialization.DataMember]
    public string AttributeName { get; set; }

    [System.Runtime.Serialization.DataMember]
    public string AttributeValue { get; set; }

   }

Now , I want to access the attributes using DataContractJsonSerializer and memorystream, but the problem is that the key '1234xxxxxx' in my json is dynamically generated everytime. So how should I access the attributes in my c# code?

dbc
  • 104,963
  • 20
  • 228
  • 340
  • Is `DataContractJsonSerializer` a hard requirement for you? Cannot you use Json.NET? – Botond Botos Nov 13 '16 at 14:58
  • You can string.replace than deserialize. – Miguel Nov 13 '16 at 16:51
  • Looks like a duplicate of [JSON deserialization of variable named parameter using DataContract](https://stackoverflow.com/questions/33024768/json-deserialization-of-variable-named-parameter-using-datacontract/33027529#33027529). Or just serialize a a `Dictionary>` using `UseSimpleDIctionaryFormat` as explained [here](https://stackoverflow.com/questions/4559991/any-way-to-make-datacontractjsonserializer-serialize-dictionaries-properly/27223290#27223290). – dbc Nov 13 '16 at 19:52
  • @botond.botos yes it is a hard requirement. – user2575854 Nov 14 '16 at 09:49
  • @Haksu can you explain a bit more as I am new to this :) – user2575854 Nov 14 '16 at 09:49
  • @dbc I have tried that settings feature but I didn't work out. Did it work for you ? – user2575854 Nov 14 '16 at 09:50

2 Answers2

0

If DataContractJsonSerializer is not a hard requirement for you, you could use Json.NET to deserialize the object:

JsonConvert.DeserializeObject<IDictionary<string, ICollection<attr>>>(jsonString);
Botond Botos
  • 1,202
  • 13
  • 20
0

Here data model.

public class Rootobject
    {
        public Attribute[] Attributes { get; set; }
    }

    public class Attribute
    {
        public string AttributeId { get; set; }
        public string AttributeName { get; set; }
        public string AttributeValue { get; set; }
    }

here static method that finds and replaces DynamicAttributeName

public static string FixJsonDynamicProperty(string jsonString,char matchChar,string newParamName)
        {
            if (string.IsNullOrEmpty(jsonString)|| string.IsNullOrEmpty(newParamName))
            {
                return jsonString;
            }


            var startIndex = jsonString.IndexOf(matchChar);

            var lastIndex = jsonString.IndexOf(matchChar, jsonString.IndexOf(matchChar) + 1);

            if (startIndex >= 0 && lastIndex > startIndex)
            {
                var dynamicParamName = jsonString.Substring(startIndex, lastIndex - startIndex + 1);

              return jsonString.Replace(dynamicParamName, newParamName);

            }

            return jsonString;
        }

and this how you use it

var jsonString ="{\"'1234xxxxxx'\":[{\"AttributeId\":\"1\",\"AttributeName\":\"Brand\",\"AttributeValue\":\"\"},{\"AttributeId\":\"2\",\"AttributeName\":\"Color\",\"AttributeValue\":\"Red4\"},{\"AttributeId\":\"3\",\"AttributeName\":\"Size\",\"AttributeValue\":\"44\"},{\"AttributeId\":\"4\",\"AttributeName\":\"Resolution\",\"AttributeValue\":\"Full HD\"}]}";

    var newJsonString = FixJsonDynamicProperty(jsonString,'\'',nameof(Rootobject.Attributes));

    Console.WriteLine(newJsonString);

you can deserialize newJsonStringwithout problem. New PropertyName matches in RootObject.Attributes . Also FixJsonDynamicProperty can be simplified with RegExp .

Miguel
  • 150
  • 7