Angularjs 在mvc中反序列化json文件数据

Angularjs 在mvc中反序列化json文件数据,angularjs,model-view-controller,Angularjs,Model View Controller,我的本地路径sendmail.json中有一个json文件,其中包含如下数据 { "from": { "datatype": "String", "required": false }, "subject": { "datatype": "String", "required": false }, "text": { "datatype": "String", "required": false }, "to": { "datatype":

我的本地路径sendmail.json中有一个json文件,其中包含如下数据

{
"from": {
    "datatype": "String",
    "required": false
},
"subject": {
    "datatype": "String",
    "required": false
},
"text": {
    "datatype": "String",
    "required": false
},
"to": {
    "datatype": "String",
    "required": false
}
}


我想在mvc中反序列化此数据,我想将此数据传递给.js angular controller。请提供方法。

如果要反序列化JSON并将其发送到客户端,可以这样做

[HttpGet]
public ActionResult mailData()
{
    List<test> myDeserializedObjList = (List<test>)Newtonsoft.Json.JsonConvert.DeserializeObject(Request["jsonString"], typeof(List<test>));
    return Json(jsonString, JsonRequestBehavior.AllowGet);
}
[HttpGet]
公共操作结果mailData()
{
List myDeserializedObjList=(List)Newtonsoft.Json.JsonConvert.DeserializeObject(请求[“jsonString”],类型(List));
返回Json(jsonString,JsonRequestBehavior.AllowGet);
}

希望它对你有用

无需将JSON反序列化为对象,然后将对象序列化为JSON以发送到浏览器

为什么不直接将JSON文件发送到浏览器

public class MailController : Controller
{
    // GET: Mail
    public ActionResult Schema()
    {
        return File(this.Server.MapPath("~/App_Data/sendmail.json"), "application/json");
    }
}
编辑:

无论如何,这里有你想要的:

public class MailController : Controller
{
    // GET: Mail
    public ActionResult Schema()
    {
        using (var reader = new StreamReader(this.Server.MapPath("~/App_Data/sendmail.json")))
        {
            var json = reader.ReadToEnd();
            var data = JsonConvert.DeserializeObject<Dictionary<string, PropertyDefinition>>(json);
            return this.Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}

public class PropertyDefinition
{
    public string datatype;
    public bool required;
}
公共类MailController:Controller
{
//获取:邮件
公共操作结果架构()
{
使用(var reader=newstreamreader(this.Server.MapPath(“~/App\u Data/sendmail.json”))
{
var json=reader.ReadToEnd();
var data=JsonConvert.DeserializeObject(json);
返回this.Json(数据,JsonRequestBehavior.AllowGet);
}
}
}
公共类属性定义
{
公共字符串数据类型;
需要公共图书馆;
}

它需要nuget package

什么是测试如何编写测试类test是一个可以从json数据生成并在列表中使用的模型,如果您想生成json数据的模型,请尝试以下方法。您可能想分享您迄今为止已经尝试过的内容。发帖时还要检查语法和标点符号。