.net - How to deserialize comments from JIRA webhook correctly -
i deserializing webhook url jira having trouble deserializing comments part of issue in controller in mvc application.
currently in deserializing correctly except comments part of json payload.
this view of comments looks in 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 , copying classes needed , comments looked this:
then classes this:
public class rootobject public property expand string public property id string public property self string public property key string public property fields fields end class public class fields public property comment comment end class public class comment public property startat integer public property maxresults integer public property total integer public property comments list(of comment2) end class public class comment2 public property body string public property created date public property updated date end class
i unsure how deserialize tried doing this:
dim reader system.io.streamreader = new system.io.streamreader(httpcontext.request.inputstream) dim rawsendgridjson string = reader.readtoend() dim issue rootobject = jsonconvert.deserializeobject(of rootobject)(rawsendgridjson)
this works else apart comments , getting error message:
cannot deserialize current json array (e.g. [1,2,3]) type 'class.comment1' because type requires json object(e.g.) {"name":"value"}) deserialize correctly.
how should deserialize correctly?
the rootobject json looks :
"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 id or key rootobject in "fields" section getting comments give me error. can information "fields" section e.g can display summary or created.
the problem number of comments e.g there 9 , way deserializing not correct handle this.
try this link problem comment, contains json paylaod verified
the json provided in commentary link not match classes in question. instance, rootobject
class not match container @ all, instead issue
class except "expand" property:
{ "timestamp": 1437140952183, "webhookevent": "jira:issue_updated", "user": { ... , "displayname": "ciaran", "active": true, "timezone": "europe/london" }, "issue": {... }, "comment": {... }...
based on jsfiddle json, root (which can renamed) should defined as:
public class ciaranobj public property timestamp long public property webhookevent string public property user actor public property issue issue public property comment commentitem end class
important note: jsfiddle sample provided may or may not complete. there number of undefined types in various classes. fields
instance has several types nulls resulting in:
property customfield_10070 object
object
may not best type when data present. likewise, timetracking
wholly undefined.
apparently, not interested in comments. if true, parse json. if @ formatted json in jsonlint.com, can see relationships. so, comments:
dim jstr = ' whereever ' extract comments reduce typing , typos dim comments = myj("issue")("fields")("comment") ' comment count dim commentcount = convert.toint32(comments("maxresults")) 'print them n int32 = 0 commentcount - 1 console.writeline("id: {0}, created: {1} upd author: {2}", comments("comments")(n)("id").tostring, comments("comments")(n)("created").tostring, comments("comments")(n)("author")("displayname").tostring) next
output:
id: 72430, created: 6/16/2015 8:48:52 upd author: ciaran id: 72431, created: 6/16/2015 9:02:16 upd author: ciaran id: 72661, created: 6/18/2015 9:58:12 upd author: ciaran
as may have noticed in formatted jsonlint display, comments buried deeper root. perhaps there other processing similar above, isnt mentioned.
to deserialize object, need use classes. can drop properties not care about, cant migrate properties 1 class another. using of robots -- json2csharp, jsonutils.com1 creates vb classes or visual studio 2012 or later (edit - paste special - paste json classes
-- auto-create classes, may want take moment "normalize" classes.
- the
user
class repeats on , onuser
,assignee
,creator
etc. reduced 1 type (class) namedactor
. avatarurls
repeats on , onavatarurls1
etc. need 1 type.- there
comment
type infields
comment
includes collection of comment items, renamed thesecommentitem
commentitem
used in root object, robots callcomment2
show in first json snippet- the
comments
property can defined array orlist<t>
shown
keep in mind, can remove property dont care (but cannot move or rename them). did:
public class ciaranobj public property timestamp long public property webhookevent string public property user actor public property issue issue public property comment commentitem end class public class actor public property self string public property name string public property key string public property emailaddress string public property avatarurls avatarurls public property displayname string public property active boolean public property timezone string end class public class avatarurls ' provide property mapping illgal names <jsonproperty("48x48")> public property _48x48 string <jsonproperty("24x24")> public property _24x24 string <jsonproperty("16x16")> public property _16x16 string <jsonproperty("32x32")> public property _32x32 string end class public class issue public property id string public property self string public property key string public property fields fields end class public class fields 'public property issuetype issuetype ' not shown ' ... public property summary string public property creator actor public property reporter actor public property progress progress ' not shown public property comment comment end class public class comment public property startat integer public property maxresults integer public property total integer public property comments commentitem() ' or: 'public property comments list(of commentitem) end class public class commentitem public property self string public property id string public property author actor public property body string public property updateauthor actor public property created datetime public property updated datetime end class
whew! now, can deserialize ciaranobj
object:
dim jstr = ...from whereever dim complexj = jsonconvert.deserializeobject(of ciaranobj)(jstr) ' reduce typing dim c comment = complexj.issue.fields.comment n int32 = 0 c.maxresults - 1 console.writeline("id: {0}, created: {1} upd author: {2}", c.comments(n).id.tostring, c.comments(n).created.tostring, c.comments(n).author.displayname) next
it prints same info.
again, works fine on jsfiddle provided in comments. has been sanitized , in course of that, other changes may have been wrought (the missing "expand" instance).
1 jsonutils.com pretty cool in can create vb classes, runs out of steam on json: classes generated incomplete. apparently due size.
Comments
Post a Comment