如何从本地存储的文件读取JSON? [英] How can I read JSON from a file stored locally?

查看:94
本文介绍了如何从本地存储的文件读取JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JSON.Net来加载本地存储在ASP.Net MVC 4站点上的JSON文件,但是无法指向该文件.这是我正在尝试做的事情:

I am attempting to use JSON.Net to load in a JSON file stored locally on an ASP.Net MVC 4 site, but am having trouble pointing to the file. Here is what I am trying to do:

List<Treatment> treatments = JsonConvert.DeserializeObject<List<Treatment>>(Server.MapPath("~/Content/treatments.json"));

并遇到此错误:

An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Unexpected character encountered while parsing value: c. Path '', line 0, position 0.

我应该怎么做?

推荐答案

您需要先使用FileStream读取JSON.

You need to read in the JSON first using a FileStream.

尝试一下.

using(StreamReader sr = new StreamReader(Server.MapPath("~/Content/treatments.json")))
{
      treatments = JsonConvert.DeserializeObject<List<Treatment>>(sr.ReadToEnd());
}

这篇关于如何从本地存储的文件读取JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆