How do I parse a JSON object in C# when I don't know key and properties don't have names? -


here unparsed json:

{ "1": [ [ [ 2015, 6, 1, 8, 0, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 1, 9, 30, 0, 3600, "europe/london", "bst", 1 ] ], [ [ 2015, 6, 1, 8, 30, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 1, 10, 0, 0, 3600, "europe/london", "bst", 1 ] ], [ [ 2015, 6, 1, 9, 0, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 1, 10, 30, 0, 3600, "europe/london", "bst", 1 ] ], [ [ 2015, 6, 1, 9, 30, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 1, 11, 0, 0, 3600, "europe/london", "bst", 1 ] ] ], "2": [ [ [ 2015, 6, 2, 8, 0, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 2, 9, 30, 0, 3600, "europe/london", "bst", 1 ] ], [ [ 2015, 6, 2, 8, 30, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 2, 10, 0, 0, 3600, "europe/london", "bst", 1 ] ], [ [ 2015, 6, 2, 9, 0, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 2, 10, 30, 0, 3600, "europe/london", "bst", 1 ] ], [ [ 2015, 6, 2, 9, 30, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 2, 11, 0, 0, 3600, "europe/london", "bst", 1 ] ] ], "3": [ [ [ 2015, 6, 3, 8, 0, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 3, 9, 30, 0, 3600, "europe/london", "bst", 1 ] ], [ [ 2015, 6, 3, 8, 30, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 3, 10, 0, 0, 3600, "europe/london", "bst", 1 ] ], [ [ 2015, 6, 3, 9, 0, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 3, 10, 30, 0, 3600, "europe/london", "bst", 1 ] ], [ [ 2015, 6, 3, 9, 30, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 3, 11, 0, 0, 3600, "europe/london", "bst", 1 ] ] ], "4": [ [ [ 2015, 6, 4, 8, 0, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 4, 9, 30, 0, 3600, "europe/london", "bst", 1 ] ], [ [ 2015, 6, 4, 8, 30, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 4, 10, 0, 0, 3600, "europe/london", "bst", 1 ] ], [ [ 2015, 6, 4, 9, 0, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 4, 10, 30, 0, 3600, "europe/london", "bst", 1 ] ], [ [ 2015, 6, 4, 9, 30, 0, 3600, "europe/london", "bst", 1 ], [ 2015, 6, 4, 11, 0, 0, 3600, "europe/london", "bst", 1 ] ] ]}

this basic format:

root

 day of month        time slot             time slot start             time slot end 

this looks formatted:

json viewer

i cannot figure out life of me how drill through this. question should similar how parse json object in c# when don't know key in advance?

if following, "system.invalidoperationexception: cannot access child value on newtonsoft.json.linq.jvalue."

        jobject objtimes = jobject.parse(strjson);          foreach (var day in objtimes["1"])         {             divtimes.innerhtml += day[0][0][0];         } 

what can design class (named, e.g., entry) represent data representing lowest level of array in json: [ 2015, 6, 3, 9, 30, 0, 3600, "europe/london", "bst", 1 ]. create jsonconverter load array values sequentially array. deserialize outer dictionary , arrays appropriate:

here 1 possible class represent data of single inner array:

[jsonconverter(typeof(entryconverter))] public class entry {     public entry()     {         // need determine whether time numbers in json in utc or in timezone given in "timezone" field.  if         // local, change datetimekind.local         datetime = new datetime(0, datetimekind.utc);      }      public datetime datetime { get; set; }     public int number { get; set; } // 3600 thing.  no idea it's for.     public string destination { get; set; }     public string timezone { get; set; }     // not time zone offsets integers: https://en.wikipedia.org/wiki/utc%e2%88%9204:30     public decimal gmtoffset { get; set; } } 

next, converter:

public class entryconverter : jsonconverter {     public override bool canconvert(type objecttype)     {         return typeof(entry).isassignablefrom(objecttype);     }      void setfield(entry entry, int index, jvalue value)     {         switch (index)         {             case 0:                 entry.datetime = new datetime((int)value, entry.datetime.month, entry.datetime.day, entry.datetime.hour, entry.datetime.minute, entry.datetime.second, entry.datetime.millisecond, entry.datetime.kind);                 break;             case 1:                 entry.datetime = new datetime(entry.datetime.year, (int)value, entry.datetime.day, entry.datetime.hour, entry.datetime.minute, entry.datetime.second, entry.datetime.millisecond, entry.datetime.kind);                 break;             case 2:                 entry.datetime = new datetime(entry.datetime.year, entry.datetime.month, (int)value, entry.datetime.hour, entry.datetime.minute, entry.datetime.second, entry.datetime.millisecond, entry.datetime.kind);                 break;             case 3:                 entry.datetime = new datetime(entry.datetime.year, entry.datetime.month, entry.datetime.day, (int)value, entry.datetime.minute, entry.datetime.second, entry.datetime.millisecond, entry.datetime.kind);                 break;             case 4:                 entry.datetime = new datetime(entry.datetime.year, entry.datetime.month, entry.datetime.day, entry.datetime.hour, (int)value, entry.datetime.second, entry.datetime.millisecond, entry.datetime.kind);                 break;             case 5:                 entry.datetime = new datetime(entry.datetime.year, entry.datetime.month, entry.datetime.day, entry.datetime.hour, entry.datetime.minute, (int)value, entry.datetime.millisecond, entry.datetime.kind);                 break;             case 6:                 entry.number = (int)value;                 break;             case 7:                 entry.destination = (string)value;                 break;             case 8:                 entry.timezone = (string)value;                 break;              case 9:                 entry.gmtoffset = (decimal)value;                 break;             default:                 throw new indexoutofrangeexception(index.tostring());         }     }      public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer)     {         var array = jarray.load(reader);         if (array == null)             return existingvalue;         var entry = (existingvalue entry ?? new entry());         (int = 0; < array.count; i++)         {             setfield(entry, i, (jvalue)array[i]);         }         return entry;     }      public override bool canwrite { { return false; } }      public override void writejson(jsonwriter writer, object value, jsonserializer serializer)     {         throw new notimplementedexception();     } } 

and use like:

        var root = jsonconvert.deserializeobject<dictionary<int, list<list<entry>>>>(json);         debug.writeline(jsonconvert.serializeobject(root, formatting.indented)); 

here compressed time information single datetime conciseness, though might not want this. also, it's unclear whether time information in utc or in local time. if local use of datetime.kind wrong.

you extend converter serialize fields in same order if needed.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -