0

Seen some ideas in other posts, but could not get rid of the Key Value text in my json string. Have a bunch of options built in code to pass to the FLOT javascript graphing app. Dictionary type items, is giving me an issue.

Using .Net 4.0, so don't have the newer DataContractJsonSerializer.

[DataContract]
public class MyOptions
{

    [DataMember(Name = "data", EmitDefaultValue = false)]
    public List<KeyValuePair<string, string>> SeriesData { get; set; }

}

Output example:

{"data":[{"key":"ooo1","value":"ppp1"},{"key":"ooo2","value":"ppp2"},{"key":"o oo3","value":"ppp3"}]}

What i need

{"data":[["ooo1","ppp1"],["ooo2","ppp2"],["ooo3","ppp3"]]}

dbc
  • 104,963
  • 20
  • 228
  • 340
Yogurt The Wise
  • 4,379
  • 4
  • 34
  • 42

1 Answers1

0

Found a solution using DataContractJsonSerializer.

Using a jagged array. String[][] not String[,](this will not serialize).

So i have ease of using the KeyValau pair when building the options, and was still able to use DataContractJsonSerializer.

        private List<KeyValuePair<string, string>> _data = new List<KeyValuePair<string, string>>();
        public List<KeyValuePair<string, string>> DataKvp
        {
            get { return _data;}
            set { _data = value; }
        }

        private String[][] _dataArray;
        [DataMember(Name = "data", EmitDefaultValue = false)]
        public String[][] DataArray
        {
            get
            {
                _dataArray = new string[_data.Count][];
                var i = 0;
                for (var index = 0; index < _data.Count; index++)
                {
                    var pair = _data[index];
                    _dataArray[i] = new[] { pair.Key, pair.Value };
                    i++;
                }
                return _dataArray;
            }

        }

So now i can still use

        test.DataKvp = new List<KeyValuePair<string, string>>();
        test.DataKvp.Add(new KeyValuePair<string, string>("ooo1", "ppp1"));
        test.DataKvp.Add(new KeyValuePair<string, string>("ooo2", "ppp2"));
        test.DataKvp.Add(new KeyValuePair<string, string>("ooo3", "ppp3"));

And the serializer will use the jagged array, and do the conversion for me.

Yogurt The Wise
  • 4,379
  • 4
  • 34
  • 42