40169

Deserializing a complex JSON object

Question:

I use JavaScript.Serializer.Deserializer to deserialize a complex JSON object, as below:

{ "name": "rule 1", "enabled": true, "conditions": [{ "type": "time", "time": "17:23:10", "days": "125" }, { "type": "sensor", "uid": "10.2.0.1", "setpoint1": 12, "setpoint2": 18, "criterion": 1 }, { "type": "sensor", "uid": "10.3.0.1", "setpoint1": 12, "setpoint2": 18, "criterion": 2 }], "actions": { "period": 100, "single": false, "act": [{ "type": "on", "uid": "10.1.0.1" }, { "type": "sms", "message": "Hello World" }] } }

And I want to convert it to some classes, like below:

public class Rule { public string name { get; set; } public bool enabled { get; set; } public List<Condition> conditions { get; set; } public List<Action> actions { get; set; } } public class Condition { public string type { get; set; } public string uid { get; set; } public DateTime? time { get; set; } public string days { get; set; } public double setpoint1 { get; set; } public double setpoint2 { get; set; } public int criterion { get; set; } } public class Action { public int period { get; set; } public bool single { get; set; } public List<Act> act { get; set; } } public class Act { public string type { get; set; } public string uid { get; set; } public string message { get; set; } }

The deserialization snippet:

json = new JavaScriptSerializer(); Rule rule = (json.Deserialize<Rule>(jsonStr));

If I simplify the Rule class and declare conditions and actions as simple strings, it works fine.

But if I use the classes like above, it throws an exception:

<blockquote>

Cannot convert object of type 'System.String' to type 'System.Collections.Generic.List`1[IoTWebApplication.Condition]'

</blockquote>

Answer1:

The structure you create does not fit to the JSON you posted.

It should look like

public class Rule { public string name { get; set; } public bool enabled { get; set; } public Condition[ ] conditions { get; set; } public Actions actions { get; set; } } public class Actions { public int period { get; set; } public bool single { get; set; } public Act[ ] act { get; set; } } public class Act { public string type { get; set; } public string uid { get; set; } public string message { get; set; } } public class Condition { public string type { get; set; } public string time { get; set; } public string days { get; set; } public string uid { get; set; } public int setpoint1 { get; set; } public int setpoint2 { get; set; } public int criterion { get; set; } }

It is (in most cases) very easy in VS to get the classes direct out of the JSON

<ul><li>Copy JSON to clipboard</li> <li>In VS <a href="http://blog.codeinside.eu/2014/09/08/Visual-Studio-2013-Paste-Special-JSON-And-Xml/" rel="nofollow">EDIT/Special Paste/Paste JSON as Classes</a> (the code above was created by this)</li> </ul>

Recommend

  • JSchException: UnknownHostKey
  • overload virtual function with different parameters in c++
  • how to keep a control disabled till a thread ends
  • Sending and Receiving SMS [closed]
  • access to sms inbox
  • Does derived class' member functions inherit virtualness from base class?
  • Difference between “&” and std::reference_wrapper?
  • Dynamically Impersonate a remote user - c# and asp.net
  • Samsung Galaxy Tab 10.1 and -webkit-tap-highlight-color style?
  • An unexpected error has been detected by HotSpot Virtual Machine
  • Two columns in subquery in where clause
  • How to extract a number from a string [duplicate]
  • UML diagram generator in Visual Studio 2010
  • Is there a way to link a linux's thread TID and a pthread_t “thread ID”
  • Graceful pod termination
  • Pythons argparse default value doesn't work
  • Fully customized Python Help Usage
  • Watson Conversation - Why is the ANYTHING ELSE node not chosen
  • Replace last two characters in column
  • pickle.PicklingError: args[0] from __newobj__ args has the wrong class with hadoop python
  • How to implement Deep Linking in Roku SG application?
  • sweetalert2 inputoptions from file in select example
  • Using $compile in a directive triggers AngularJS infinite digest error
  • Switch to popup in python using selenium
  • CSS bleed-through with cfinput type=“datefield”
  • Find group of records that match multiple values
  • Possible to get mouse events fired when cursor is outside page?
  • What and where is mdimport
  • NHibernate Validation Localization with S#arp Architecture
  • How can I send an e-mail from a vbs script
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Accessing IRQ description array within a module and displaying action names
  • Where to put my custom functions in Wordpress?
  • Redux, normalised entities and lodash merge
  • Do create extension work in single-user mode in postgres?
  • Getting Messege Twice Using IMvxMessenger
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs