I am currently deserializing a webhook URL from JIRA but I am having trouble with deserializing the comments part of the issue in a controller in a MVC application.
Currently in I am deserializing everything correctly except for the comments part of the Json payload.
This is a view of what the comments looks like in the JSON Payload:
'comments':[
{'id':'71980','displayName':'Ciaran','active':true},'created':'2015-06-10T09:30:07.983+0100','updated':'2015-06-10T09:30:07.983+0100'},
{'id':'72026','displayName':'Ciaran ','active':true},'created':'2015-06-10T14:50:34.253+0100','updated':'2015-06-10T14:50:34.253+0100'}]
Using Json2CSharp and then copying the classes I needed and the Comments looked like this:
Then the classes look like this:
Public Class Rootobject
Public Property expand As String
Public Property id As String
Public Property self As String
Public Property key As String
Public Property fields As Fields
End Class
Public Class Fields
Public Property comment As Comment
End Class
Public Class Comment
Public Property startAt As Integer
Public Property maxResults As Integer
Public Property total As Integer
Public Property comments As List(Of Comment2)
End Class
Public Class Comment2
Public Property body As String
Public Property created As Date
Public Property updated As Date
End Class
I am unsure about how I would deserialize this properly I tried doing this:
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(HttpContext.Request.InputStream)
Dim rawSendGridJSON As String = reader.ReadToEnd()
Dim issue As Rootobject = JsonConvert.DeserializeObject(Of Rootobject)(rawSendGridJSON)
This works for everything else apart from the comments and I am getting this error message:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'class.Comment1' because the type requires a JSON object(e.g.) {"name":"value"}) to deserialize correctly.
How should I deserialize this correctly?
The rootObject Json looks like this :
"expand":"renderedFields,names,schema,transitions,operations,editmeta,changelog,
"id": "41948",
"self": "http://jira:8080/rest/api/latest/issue/41948",
"key": "OP-155",
"fields": {
"comment": {
"startAt": 0,
"maxResults": 9,
"total": 9,
"comments": []
}
}
I can successfully get the ID or key from the rootObject but in the "fields" section getting the comments will give me the error. But I can get information from the "fields" section e.g I can display the summary or created.
The problem with the number of comments e.g there are nine and the way I am deserializing is not correct to handle this.
try this link the problem is with the comment, that contains the json paylaod it is verified
The json provided in the commentary link does not match the classes in the question. For instance, your
RootObjectclass does not match the container at all, but instead is mostly theIssueclass except for the "expand" property:Based on the jsfiddle json, the root (which can be renamed) should defined as:
Important Note: The jsfiddle sample provided may or may not be complete. There are a number of undefined Types in the various classes.
Fieldsfor instance has several Types with nulls resulting in:Objectmay not be the best Type when data is present. Likewise,TimeTrackingis wholly undefined.Apparently, you are not interested in anything but the comments. If true, you could just parse the json. If you look at the formatted json in something like jsonlint.com, you can see the relationships. So, to just get the comments:
Output:
As you may have noticed in the formatted JsonLint display, the comments are actually buried somewhat deeper than the root. Perhaps there was some other processing similar to the above, but it isnt mentioned.
To deserialize to an object, you need to use classes. You can drop any properties you do not care about, but you cant migrate properties from one class to another. Using any of the robots -- json2csharp, jsonutils.com1 which creates VB classes or even Visual Studio 2012 or later (
Edit - Paste Special - Paste as Json Classes-- to auto-create the classes, you may want to take a moment to "normalize" the classes.Userclass repeats over and over forUser,Assignee,Creatoretc. I reduced it to one Type (Class) namedActor.AvatarUrlsalso repeats over and over asAvatarUrls1etc. You only need one Type.CommentType inFieldsCommentalso includes a collection of comment items, I renamed theseCommentItemCommentItemis also used in the root object, but the robots will call itComment2This is what you show in the first json snippetcommentsproperty can be defined as an array or aList<T>as shownKeep in mind, you can remove any property you dont care about (but you cannot move or simply rename them). Which I did:
Whew! Now, you can deserialize to a
CiaranObjobject:It prints the same info.
Again, this works fine on the jsfiddle provided in the comments. It has clearly been sanitized and in the course of that, other changes may have been wrought (the missing "expand" for instance).
1 jsonutils.com is pretty cool in that it can create VB classes, but it actually runs out of steam on this json: the classes generated are incomplete. This is apparently due to the size.