构建包含JArray的JSON内联

时间:2018-05-17 10:33:40

标签: c# json.net

我需要像这样创建一个JSON结构

{
"update": {
    "comment": [
        {
            "add": {
                "body": "Bug has been fixed."
            }
        }
    ]
},
"fields": {
    "assignee": {
        "name": "bob"
    },
    "resolution": {
        "name": "Fixed"
    }
},
"transition": {
    "id": "5"
}

为了构建这个,我在c#

中尝试了以下代码
        JObject issue_model = JObject.FromObject(new
        {
            update = new
            {
               comment = new JArray
                {
                 //How to add JArray contents here inline    
                }
            },
            fields = new
            {
                resolution = new
                {
                    name = "namem vakye gere"
                }
            },
            transition = new
            {
                id= transition_id
            }
        });

但是我们如何添加JArray内容"添加" &安培; "主体"零件内联?

1 个答案:

答案 0 :(得分:1)

您不能直接使用JArray,您可以在外部匿名对象中创建匿名对象数组,header然后转换为数组:

FromObject

哪个输出这个Json:

string transition_id = "5";

JObject issue_model = JObject.FromObject(new
{
    update = new
    {
       comment = new[] { 
            new {
                add = new { 
                   body = "bug has been fixed" 
                }
            }
        }
     },
     fields = new
     {
         resolution = new { 
             name = "namem vakye gere" 
         }
     },
     transition = new { 
        id = transition_id 
    }
}
);