如何打开json文件并将其解析为Json数组(JArray)?

时间:2017-02-07 06:12:49

标签: c# asp.net json asp.net-mvc

我将c#asp.net mvc中的Web API控制器放在一起打开一个Json文件,该文件的Json数组如下所示:

[ 
  {
    "age": 0, 
    "id": "motorola-xoom-with-wi-fi"
  }, 
  {
    "age": 1, 
    "id": "motorola-xoom"
  },
  ...
  ... 
 ] 

然后我尝试将其解析为Json数组,最后将其作为JsonResult返回给API调用者。

1 个答案:

答案 0 :(得分:3)

这是我最终做的事情:

// GET api/values
    [HttpGet]
    public JsonResult Get()
    {
        // read JSON directly from a file
        using (StreamReader file = System.IO.File.OpenText(@"wwwroot/phones/phones.json"))
        {
            var jArray = JArray.Parse(file.ReadToEnd());
            return Json(jArray); 
        }
    }

干杯!