Reputation: 387
I am using json.net to deserialise a json string into a classed object for use in a vb.net console application. I have started to set up classes for each of these fields but I am a bit confused as to how I can set up the sta class as it appears to be an array of a list?
I would appreciate any help you can provide, the plan is to use the Json.JsonConvert.DeserializeObject to deserialize the string into the class. I will error handle as some strings don't have any strokes, just the time.
{"time":1423625141,"id":35609390,"strokes":[{"time":1423625137180,"lat":-34.4798
08,"lon":147.249959,"alt":0,"cur":0,"dev":6553,"inv":0,"sta":[9,[812,849,919,118
5,874,1248,984,1276,875]],"id":35609386},{"time":1423625137250,"lat":-34.542924,
"lon":147.299573,"alt":0,"cur":0,"dev":7505,"inv":0,"sta":[9,[812,849,877,919,11
85,874,1248,984,1276,875]],"id":35609388},{"time":1423625137552,"lat":-34.514597
,"lon":147.284943,"alt":0,"cur":0,"dev":6894,"inv":0,"sta":[7,[849,919,1185,1248
,984,1276,1016,875]],"id":35609389}]}
My Class is below:
Class Data
Public time As Integer
Public id As Integer
Public strokes As List(Of Strokes)
End Class
Class Strokes
Public time As Integer
Public lat As Decimal
Public lon As Decimal
Public alt As Integer
Public cur As Integer
Public dev As Integer
Public sta As New sta
End Class
Class sta
Public sta As List(Of Integer)
End Class
Upvotes: 0
Views: 72
Reputation: 475
Even though you have a complex object sent via JSON the DefaultModelBinder should pick it up and map it to your Model. There are a couple things you need to make sure of though.
The first is how you send your JSON. The content-type should be explicity set to "application/json" and if using JQuery you need to use the $ajax method (not $post) and be sure to stringify to format the data properly). I also use Fiddler to catch the JSON and run it through an online validator to be sure it is passing correctly formatted.
$.ajax( {
type: "POST",
url: /MyJsonActionController',
contentType: 'application/json; charset=utf-8',
data: Json.stringify(MyFormData)
}
Secondly, Your action controller should simply state the model to be mapped to in the constructor Ex:
Public Function MyJsonActionController(ByVal d as Data) JsonResult
'ModelBinder should do all of the work and then you can use the incoming model to do what you'd like
SaveToDatabase(d)
return Json(true)
End Function
Also be sure to name the form fields according to the Model value names. That should do it.
If you run into any trouble, this is a great article explaining very clearly how ModelBinding works. You will see that according to this article the ModelBinder will make a first pass to read all of the primitive types to map the model and if there are more complex types it will recursively make another pass and pick those up and map them. The DefaultModelBinder can actually handle complex/nested model mapping out of the box if the JSON is presented correctly.
https://msdn.microsoft.com/en-us/magazine/hh781022.aspx
Upvotes: 1