c# - Jira post error - expecting comma to separate OBJECT entries -
i trying use below code create new project issue in jira,
using using system.text; using system.net.http; using system.json; using system.web.script.serialization; namespace test { class class1 { public void createissue() { string message = "hai \"!hello\" "; string data = "{\"fields\":{\"project\":{\"key\":\"tp\"},\"summary\":\"" + message + "\",\"issuetype\":{\"name\": \"bug\"}}}"; system.net.http.httpclient client = new system.net.http.httpclient(); client.defaultrequestheaders.expectcontinue = false; client.timeout = timespan.fromminutes(90); byte[] crdential = utf8encoding.utf8.getbytes("adminname:adminpassword"); client.defaultrequestheaders.authorization = new system.net.http.headers.authenticationheadervalue("basic", convert.tobase64string(crdential)); client.defaultrequestheaders.accept.add(new system.net.http.headers.mediatypewithqualityheadervalue("application/json")); system.net.http.httpcontent content = new stringcontent(data, encoding.utf8, "application/json"); try { client.postasync("http://localhost:8080/rest/api/2/issue",content).continuewith(requestask=> { try { httpresponsemessage response = requestask.result; response.ensuresuccessstatuscode(); response.content.readasstringasync().continuewith(readtask => { var out1 = readtask.result; }); } catch (exception exception) { console.writeline(exception.stacktrace.tostring()); console.readline(); } }); } catch (exception exc) { } } } } it throws error below:
"{\"errormessages\":[\"unexpected character ('!' (code 33)): expecting comma separate object entries\n @ [source: org.apache.catalina.connector.coyoteinputstream@1ac4eeea; line: 1, column: 52]\"]}"
but use same json file in firefox poster , have created issue.
json file : {"fields":{"project":{"key":"tp"},"summary":"hai \"!hello\" ",\"issuetype\":{\"name\": \"bug\"}}}"
so, what's wrong code?
there appear issues escaping quotation marks.
here pretty-printed version of data variable after initialized:
{ "fields":{ "project":{ "key":"tp" }, "summary":"hai "!hello" ", "issuetype":{ "name": "bug" } } } the error message says unexpected character ('!')...was expecting comma.... looking @ pretty-printed string, reason error becomes clear:
"summary":"hai "!hello" ", the json interpreter used jira parses text this:
"summary":"hai " ⇐ trailing quotation mark ends "summary" value ! ⇐ json expects comma here hello" ", in c# code, have this:
string message = "hai \"!hello\" "; the quotation mark before exclamation mark escaped backslash. however, escapes quotation mark c# compiler. when c# compiler done it, backslash gone. embed quotation mark in json string, backslash followed quotation mark needed:
string message = "hai \\\"!hello\\\" "; to avoid lot of gotchas related json formatting, highly recommend using microsoft's javascriptserializer class. class provides safe, fast way create valid json:
using system; using system.collections.generic; using system.web.script.serialization; namespace jsontests { class program { static void main(string[] args) { string message = "hai \"!hello\" "; var project = new dictionary<string, object>(); project.add("key", "tp"); var issuetype = new dictionary<string, object>(); issuetype.add("name", "bug"); var fields = new dictionary<string, object>(); fields.add("project", project); fields.add("summary", message); fields.add("issuetype", issuetype); var dict = new dictionary<string, object>(); dict.add("fields", fields); javascriptserializer serializer = new javascriptserializer(); string json = serializer.serialize((object)dict); console.writeline(json); } } } result:
{"fields":{"project":{"key":"tp"},"summary":"hai \"!hello\" ","issuetype":{"name":"bug"}}}
Comments
Post a Comment