C# MVC Return 格式化列表 Json
C# MVC Return List in Formatted Json
有一个 returns 在 Json 中的列表,但想要将其格式化如下。我这辈子都想不通为什么我似乎无法正确格式化它。
[{
"AppFormName": "TFB Test Application",
"Questions": [{
"QuestionName": "How old are you?",
"QuestionType": 1
},
{
"QuestionName": "Where are you from?",
"QuestionType": 1
}]
},
{
"AppFormName": "HLL",
"Questions": [{
"QuestionName": "How old are you?",
"QuestionType": 1
},
{
"QuestionName": "Where are you from?",
"QuestionType": 1
},
{
"QuestionName": "What Game are you applying for?",
"QuestionType": 2
},
{
"QuestionName": "Do you agree to the clan rules",
"QuestionType": 3
}]
}]
这是我的代码当前生成的结果:
[
{
"AppFormName": "TFB Test Application",
"QuestionName": "How old are you?",
"QuestionType": 1
},
{
"AppFormName": "TFB Test Application",
"QuestionName": "Where are you from?",
"QuestionType": 1
},
{
"AppFormName": "HLL",
"QuestionName": "How old are you?",
"QuestionType": 1
},
{
"AppFormName": "HLL",
"QuestionName": "Where are you from?",
"QuestionType": 1
},
{
"AppFormName": "HLL",
"QuestionName": "What Game are you applying for?",
"QuestionType": 2
},
{
"AppFormName": "HLL",
"QuestionName": "Do you agree to the clan rules",
"QuestionType": 3
}
]
这是我尝试序列化结果的控制器;
public ActionResult AccessToken(string authorizationCode)
{
UserFunctions.AccessToken(authorizationCode);
var results = UserFunctions.userApplications;
return Json(results , JsonRequestBehavior.AllowGet);
}
型号:
public class UserApplications
{
public string AppFormName { get; set; }
public string QuestionName { get; set; }
public int QuestionType { get; set; }
}
获取数据的函数;
public static List<Models.UserApplications> GetUserApplications(string ClientId)
{
userApplications.Clear();
var getUserApplications = getUserApplicationsSQL;
using (var conn = new MySqlConnection(dataConn))
{
using (var cmd = new MySqlCommand(getUserApplications, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@DiscordClientID", ClientId);
cmd.Connection.Open();
using (var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (dr.Read())
{
var u = new Models.UserApplications
{
AppFormName = dr["AppFormName"].ToString(),
QuestionName = dr["QuestionName"].ToString(),
QuestionType = dr.GetInt32(dr.GetOrdinal("TypeID")),
};
userApplications.Add(u);
}
}
}
}
return userApplications;
}
您需要代码将现有的数据结构转换为可生成所需 JSON 结构的数据结构。这可以使用 Linq 相对容易地完成。然后您需要 return 在您的操作方法中列出该列表:
public ActionResult AccessToken(string authorizationCode)
{
UserFunctions.AccessToken(authorizationCode);
var results = UserFunctions.userApplications;
var appFormList = results.GroupBy(s => s.AppFormName).Select(g => new
{
AppFormName = g.Key, Questions = g.Select(a => new
{
a.QuestionName,
a.QuestionType
})
});
return Json(appFormList, JsonRequestBehavior.AllowGet);
}
现场演示:https://dotnetfiddle.net/sRIy72
(感谢 this post 使用 Linq 的想法。)
这可以简单地通过分组来实现:
public ActionResult AccessToken(string authorizationCode)
{
UserFunctions.AccessToken(authorizationCode);
List<UserApplications> results = UserFunctions.userApplications;
var data = from p in results
group p by p.AppFormName into g
select new { AppFormName = g.Key, Questions = g.Select(x=> new { QuestionName = x.QuestionName, QuestionType = x.QuestionType}).ToList() };
return Json(data , JsonRequestBehavior.AllowGet);
}
有一个 returns 在 Json 中的列表,但想要将其格式化如下。我这辈子都想不通为什么我似乎无法正确格式化它。
[{
"AppFormName": "TFB Test Application",
"Questions": [{
"QuestionName": "How old are you?",
"QuestionType": 1
},
{
"QuestionName": "Where are you from?",
"QuestionType": 1
}]
},
{
"AppFormName": "HLL",
"Questions": [{
"QuestionName": "How old are you?",
"QuestionType": 1
},
{
"QuestionName": "Where are you from?",
"QuestionType": 1
},
{
"QuestionName": "What Game are you applying for?",
"QuestionType": 2
},
{
"QuestionName": "Do you agree to the clan rules",
"QuestionType": 3
}]
}]
这是我的代码当前生成的结果:
[
{
"AppFormName": "TFB Test Application",
"QuestionName": "How old are you?",
"QuestionType": 1
},
{
"AppFormName": "TFB Test Application",
"QuestionName": "Where are you from?",
"QuestionType": 1
},
{
"AppFormName": "HLL",
"QuestionName": "How old are you?",
"QuestionType": 1
},
{
"AppFormName": "HLL",
"QuestionName": "Where are you from?",
"QuestionType": 1
},
{
"AppFormName": "HLL",
"QuestionName": "What Game are you applying for?",
"QuestionType": 2
},
{
"AppFormName": "HLL",
"QuestionName": "Do you agree to the clan rules",
"QuestionType": 3
}
]
这是我尝试序列化结果的控制器;
public ActionResult AccessToken(string authorizationCode)
{
UserFunctions.AccessToken(authorizationCode);
var results = UserFunctions.userApplications;
return Json(results , JsonRequestBehavior.AllowGet);
}
型号:
public class UserApplications
{
public string AppFormName { get; set; }
public string QuestionName { get; set; }
public int QuestionType { get; set; }
}
获取数据的函数;
public static List<Models.UserApplications> GetUserApplications(string ClientId)
{
userApplications.Clear();
var getUserApplications = getUserApplicationsSQL;
using (var conn = new MySqlConnection(dataConn))
{
using (var cmd = new MySqlCommand(getUserApplications, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@DiscordClientID", ClientId);
cmd.Connection.Open();
using (var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (dr.Read())
{
var u = new Models.UserApplications
{
AppFormName = dr["AppFormName"].ToString(),
QuestionName = dr["QuestionName"].ToString(),
QuestionType = dr.GetInt32(dr.GetOrdinal("TypeID")),
};
userApplications.Add(u);
}
}
}
}
return userApplications;
}
您需要代码将现有的数据结构转换为可生成所需 JSON 结构的数据结构。这可以使用 Linq 相对容易地完成。然后您需要 return 在您的操作方法中列出该列表:
public ActionResult AccessToken(string authorizationCode)
{
UserFunctions.AccessToken(authorizationCode);
var results = UserFunctions.userApplications;
var appFormList = results.GroupBy(s => s.AppFormName).Select(g => new
{
AppFormName = g.Key, Questions = g.Select(a => new
{
a.QuestionName,
a.QuestionType
})
});
return Json(appFormList, JsonRequestBehavior.AllowGet);
}
现场演示:https://dotnetfiddle.net/sRIy72
(感谢 this post 使用 Linq 的想法。)
这可以简单地通过分组来实现:
public ActionResult AccessToken(string authorizationCode)
{
UserFunctions.AccessToken(authorizationCode);
List<UserApplications> results = UserFunctions.userApplications;
var data = from p in results
group p by p.AppFormName into g
select new { AppFormName = g.Key, Questions = g.Select(x=> new { QuestionName = x.QuestionName, QuestionType = x.QuestionType}).ToList() };
return Json(data , JsonRequestBehavior.AllowGet);
}