2015-11-28 43 views
1

我在我的本地路径一个JSON文件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角度控制器请提供方法。

+0

你可能想分享你已经到目前为止已经试过。同时检查您的语法和标点符号。 –

回答

1

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

[HttpGet] 
public ActionResult mailData() 
{ 
    List<test> myDeserializedObjList = (List<test>)Newtonsoft.Json.JsonConvert.DeserializeObject(Request["jsonString"], typeof(List<test>)); 
    return Json(jsonString, JsonRequestBehavior.AllowGet); 
} 

希望它适合你..!

+0

什么是测试如何编写测试类 –

+0

测试是一种模型,您可以从您的json数据中制作模型,并在列表中使用它,如果您想使您的json数据的模型尝试此操作。 http://json2csharp.com/ –

+0

它适用于我谢谢 –

0

没有必要将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; 
} 

它需要Newtonsoft.JSON NuGet包

+0

为什么这个答案有downvote? –