gpt4 book ai didi

c# - 上传 JSON 文件并解析

转载 作者:行者123 更新时间:2023-11-30 14:58:38 24 4
gpt4 key购买 nike

我正在使用自定义 Bootstrap ,链接:http://jasny.github.io/bootstrap/javascript.html#fileupload我有一个上传文件的按钮。这个文件是一个 JSON,我想在我的 Controller 上读取它。我怎样才能做到这一点?带有文件参数的方法并使用 Json 解析器读取此文件。

最佳答案

只需创建您的表单并像这样创建您的操作:

using System.Web.Script.Serialization;

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
// get contents to string
string str = (new StreamReader(file.InputStream)).ReadToEnd();

// deserializes string into object
JavaScriptSerializer jss = new JavaScriptSerializer();
var d = jss.Deserialize<dynamic>(str);

// once it's an object, you can use do with it whatever you want
}
}

关于c# - 上传 JSON 文件并解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18340419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com