创建子节点是数组的json结构

272 272
Newtonsoft.Json
Jordan
Jordan 2024-10-17 00:55:27

创建以下json结构

{
  "UserName": "xx",
  "Sex": "男",
  "like": {
    "a1": "v1",
    "a2": "v2"
  },
  "data": [
    {
      "col1": "v11",
      "col2": "v12"
    },
    {
      "col1": "v21",
      "col2": "v22"
    }
  ]
}
  • 当前结构包含data 数组,数组两列 两行组成

代码

/// <summary>
/// 创建标准的一个json
/// </summary>
/// <returns></returns>
public IActionResult Index3()
{

    JObject jObject = new JObject();
    jObject.Add("UserName", "xx");
    jObject.Add("Sex", "男");

    JObject child = new JObject();
    child.Add("a1", "v1");
    child.Add("a2", "v2");

    jObject.Add("like", child);

    //创建一个数组

    JArray jArray = new JArray();
    JObject o1 = new JObject();
    o1.Add("col1","v11");
    o1.Add("col2", "v12");
    jArray.Add(o1);
    JObject o2 = new JObject();
    o2.Add("col1", "v21");
    o2.Add("col2", "v22");
    jArray.Add(o2);

    jObject.Add("data",jArray);

    //转换为json信息
    String retInfo = jObject.ToString();
    return Content(retInfo);
}
  • 通过 Jarray与Jobject 来形成 当前的json结构
回帖
  • 消灭零回复
作者信息
主题分类
相关文章