添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to use the below code to create new project issue in my 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");
                client.PostAsync("http://localhost:8080/rest/api/2/issue",content).ContinueWith(requesTask=>
                        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 the error like below:

"{\"errorMessages\":[\"Unexpected character ('!' (code 33)): was expecting comma to separate OBJECT entries\n at [Source: org.apache.catalina.connector.CoyoteInputStream@1ac4eeea; line: 1, column: 52]\"]}"

But i use the same json file in Firefox Poster , i have created the issue.

Json file : {"fields":{"project":{"key":"TP"},"summary":"Hai \"!Hello\" ",\"issuetype\":{\"name\": \"Bug\"}}}"

So, what's wrong with my code?

There appear to be issues with escaping quotation marks.

Here is a pretty-printed version of the data variable after it is initialized:

"fields":{ "project":{ "key":"TP" "summary":"Hai "!Hello" ", "issuetype":{ "name": "Bug"

The error message says Unexpected character ('!')...was expecting comma.... Looking at the pretty-printed string, the reason for the error becomes clear:

"summary":"Hai "!Hello" ",

The JSON interpreter used by JIRA likely parses this text like this:

"summary":"Hai " ⇐ This trailing quotation mark ends the "summary" value
! ⇐ JSON expects a comma here
Hello" ", 

In your C# code, you have this:

string message = "Hai \"!Hello\" ";

The quotation mark before the exclamation mark is escaped by a backslash. However, this only escapes the quotation mark for the C# compiler. When the C# compiler is done with it, the backslash is gone. To embed a quotation mark in a JSON string, a backslash followed by a quotation mark is needed:

string message = "Hai \\\"!Hello\\\" ";

To avoid a lot of the gotchas related to JSON formatting, I highly recommend using Microsoft's JavaScriptSerializer class. This class provides a safe, fast way to 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"}}}
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.