Deserializing JSON data with no 'key'

Jun 12, 2012 at 8:55 PM

I have some JSON output that looks like this:

{
      "raw_data": [
        [
          1088553600000,
          1.3635
        ],
        [
          1091232000000,
          1.3538
        ] ]
}
How can I structure a class to deserialize this into since the values in raw_data do not have a 'key' name?

Jun 13, 2012 at 2:25 PM

Test

Jun 22, 2012 at 10:59 AM

 

I ve also problem with no-key array. My solution:

/* Class */

public
class Example { public Int32 count { get; set; } public Boolean result { get; set; } public String text { get; set; } public DateTime date { get; set; } public List<object[]> raw_data { get; set; } public Example(Int32 count, Boolean result, String text, DateTime date, List<object[]> raw_data) { this.count = count; this.result = result; this.text = text; this.date = date; this.raw_data = raw_data; } }

 

/* Main */

 

[...]
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;

private void button_Click(object sender, EventArgs e) { /* Serialize Object */ List<object[]> raw = new List<object[]>();

  raw.Add(new object[]{ 1088553600000, 1.3635M }); raw.Add(new object[]{ 1091232000000, 1.3538M }); Example em = new Example(5490, true, "Show me the way", DateTime.Now, raw); String json = JsonConvert.SerializeObject(em); richTextBox.Text = json; /* Deserialize Object */ String json_to_deserialize = @"{ ""raw_data"": [ [ 1088553600000, 1.3635 ], [ 1091232000000, 1.3538 ] ] }";
 Example em1 = JsonConvert.DeserializeObject<Example>(json_to_deserialize); richTextBox.Text = em1.raw_data.Count.ToString();
// or richTextBox.Text = em1.raw_data[0].First().ToString();
// or cast raw_data to whatever u want }


greetings
David

 

 

Jun 29, 2012 at 10:22 AM

i ve found a solution:

 

  ""raw_data"": [
      [
        1088553600000, <- `id` property in RawData class
        1.3635 <- `point` property in RawData class
      ],

 

 

using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Globalization;

namespace Test
{
    public class Example
    {
        public Int32 count { get; set; }
        public Boolean result { get; set; }
        public String text { get; set; }
        public DateTime date { get; set; }
        public List<RawData> raw_data { get; set; }

        public Example(Int32 count, Boolean result, String text, DateTime date, List<RawData> raw_data)
        {
            this.count = count;
            this.result = result;
            this.text = text;
            this.date = date;
            this.raw_data = raw_data;
        }
        public Example()
        {
        }
    }

    [JsonConverter(typeof(RawDataConverter))]
    public class RawData
    {
        public Int64 id { get; set; }
        public Double point { get; set; }

        public RawData()
        {

        }

    }

    public class RawDataConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            if (objectType == typeof(RawData))
            {
                return true;
            }

            return false;
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {

            JArray ja = new JArray();
            while (reader.Read())
            {

                if (reader.TokenType == JsonToken.EndArray) break;
                   ja.Add(reader.Value);

            }
     
            RawData rd = new RawData();

            /* There probably will be  "Input string in not correct format"  exceptions*/
            rd.id = Int64.Parse(ja[0].ToString());
            rd.point = Double.Parse(ja[1].ToString(), new CultureInfo("en-US"));
            /**/
            return rd;
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

    }
}