创建以下json结构
{
"UserName": "xx",
"Sex": "男",
"like": {
"a1": "v1",
"a2": "v2"
},
"data": [
{
"col1": "v11",
"col2": "v12"
},
{
"col1": "v21",
"col2": "v22"
}
]
}
代码
/// <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结构