using System.Collections.Generic;
using System.Diagnostics;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public string camera_clock { get; set; }
public string video_standard { get; set; }
public string app_status { get; set; }
public string stream_out_type { get; set; }
public string save_low_resolution_clip { get; set; }
public string video_resolution { get; set; }
public string video_stamp { get; set; }
public string video_quality { get; set; }
public string timelapse_video { get; set; }
public string photo_size { get; set; }
public string photo_stamp { get; set; }
public string photo_quality { get; set; }
public string timelapse_photo { get; set; }
public string selfie_photo { get; set; }
public string burst_photo { get; set; }
public string autoshoot_photo { get; set; }
public string loop_record { get; set; }
public string motion_detec_video { get; set; }
public string status_led_switch { get; set; }
public string wifi_led_switch { get; set; }
public string osd_switch { get; set; }
public string cardvr_switch { get; set; }
public string delay_pwroff { get; set; }
public string rotate_image { get; set; }
public string mic_vol { get; set; }
public string language { get; set; }
public string date_disp_fmt { get; set; }
public string auto_bkl_off { get; set; }
public string auto_pwr_off { get; set; }
public string light_freq { get; set; }
public string meter_mode { get; set; }
public string buzzer { get; set; }
public int rval { get; set; }
public int msg_id { get; set; }
public Param param { get; set; }
public class ArrayToObjectConverter<T> : JsonConverter
public override bool CanConvert(Type objectType)
return typeof(T).IsAssignableFrom(objectType);
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
if (reader.TokenType == JsonToken.Null)
if (existingValue == null)
var contract = serializer.ContractResolver.ResolveContract(objectType);
existingValue = contract.DefaultCreator();
switch (reader.TokenType)
case JsonToken.StartArray:
var jArray = JArray.Load(reader);
var jObj = new JObject();
foreach (var prop in jArray.OfType<JObject>().SelectMany(o => o.Properties()))
using (var sr = jObj.CreateReader())
serializer.Populate(sr, existingValue);
case JsonToken.StartObject:
serializer.Populate(reader, existingValue);
var msg = "Unexpected token type " + reader.TokenType.ToString();
throw new JsonSerializationException(msg);
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();
""camera_clock"": ""2015-09-03 04:42:20""
""video_standard"": ""NTSC""
""stream_out_type"": ""rtsp""
""save_low_resolution_clip"": ""off""
""video_resolution"": ""1920x1080 60P 16:9""
""video_quality"": ""S.Fine""
""timelapse_video"": ""off""
""photo_size"": ""16M (4608x3456 4:3)""
""photo_quality"": ""S.Fine""
""timelapse_photo"": ""off""
""selfie_photo"": ""off""
""autoshoot_photo"": ""off""
""motion_detec_video"": ""off""
""status_led_switch"": ""off""
""wifi_led_switch"": ""on""
""cardvr_switch"": ""off""
""delay_pwroff"": ""off""
""rotate_image"": ""normal""
""date_disp_fmt"": ""ymd""
""auto_pwr_off"": ""180""
public static void Test()
var settings = new JsonSerializerSettings { Converters = new[] { new ArrayToObjectConverter<Param>() } };
var root = JsonConvert.DeserializeObject<RootObject>(json, settings);
var json2 = JsonConvert.SerializeObject(root, Formatting.Indented, settings);
ConsoleAndDebug.WriteLine("Json deserialized successfully. Re-serialized JSON: ");
ConsoleAndDebug.WriteLine(json2);
|| root.param.video_standard != "NTSC"
throw new InvalidOperationException("incorrect deserialized values");
ConsoleAndDebug.WriteLine("Initial Json: ");
ConsoleAndDebug.WriteLine(JToken.Parse(json));
public static class ConsoleAndDebug
public static void WriteLine(object s)
public static void Main()
ConsoleAndDebug.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);