JSON Serialize数组在JSON字符串中返回NULL

问题描述 投票:1回答:1

我正在构建这样的JSON模型

JObject issue_model = JObject.FromObject(new
{
   labels = new[] { "import", "automation"}
}

下面的序列化代码

string request_json = JsonConvert.SerializeObject(issue_model,
                  Newtonsoft.Json.Formatting.Indented,
                  new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); 

但是,当我尝试从动态值列表中构建它时,如

   list<string> lp_list = new list<string>();
   //lp_list contains a list of string values
   string[] lp_labels = lp_list.ToArray();
   JObject issue_model = JObject.FromObject(new
   {
      labels = jira_labels
   }

我得到了JSON

   "labels": [
  [
    null,
    null
  ]
]

但我期待这个json

“标签”:{“import”,“automation”}

如何使阵列序列化正确

c# json json.net
1个回答
4
投票

我在控制台应用程序中修改了您的代码。

List<string> lp_list = new List<string>();
lp_list.Add("import");
lp_list.Add("automation");

//lp_list contains a list of string values
//string[] lp_labels = lp_list.ToArray();
JObject issue_model = JObject.FromObject(new
{
    labels = lp_list
});

Console.WriteLine(issue_model);

结果如下: enter image description here

希望它能回答你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.