Jira post错误-期望用逗号分隔OBJECT条目

本文关键字:分隔 OBJECT 条目 post 错误 期望 Jira | 更新日期: 2023-09-27 18:12:02

我试图使用下面的代码在我的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)
            {
            }
        }
    }
}

抛出如下错误:

"{'"errormessage '":['"意外字符("!'(代码33)):期望用逗号分隔对象项'n在[来源:org.apache.catalina.connector.CoyoteInputStream@1ac4eeea;Line: 1, column: 52]'"}"

但是我在Firefox Poster中使用相同的json文件,我已经创建了这个问题。

Json文件:{"字段":{"项目":{"关键":"TP"},"总结":"海'"!你好'",'"issuetype '":{'"'":'"错误'"}}}"

那么,我的代码出了什么问题?

Jira post错误-期望用逗号分隔OBJECT条目

转义引号似乎有问题。

这是data变量初始化后的漂亮打印版本:

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

错误信息显示Unexpected character ('!')...was expecting comma...。查看打印得很漂亮的字符串,错误的原因就很清楚了:

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

JIRA使用的JSON解释器可能会像这样解析这个文本:

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

在你的c#代码中,你有这个:

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

感叹号前的引号用反斜杠转义。然而,这只是对c#编译器转义引号。当c#编译器完成它时,反斜杠就消失了。要在JSON字符串中嵌入引号,需要一个后跟引号的反斜杠:

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

为了避免大量与JSON格式相关的陷阱,我强烈建议使用微软的JavaScriptSerializer类。这个类提供了一种安全、快速的方式来创建有效的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);
        }
    }
}
结果:

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