visual-studio-2013 - 没有服务引用的项目。错误 : Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported.

标签 visual-studio-2013 asp.net-web-api

我正在尝试编写一个包含 newtonsoft JSON.net 组件的 Web API。

我的代码非常简单:

     public object GetJsonFile(int id = 1) {
        using (StreamReader r = new StreamReader(myJsonFile)) {
            string json;

        //    todo: build logic to only grab latest when an id is supplied
        json = r.ReadToEnd();

        object jsonObject = JsonConvert.DeserializeObject(json);
        return jsonObject;
       }

测试页面时,我得到了可怕的“类型'Newtonsoft.Json.Linq.JToken'是不支持的递归集合数据契约。考虑修改集合'Newtonsoft.Json.Linq.JToken'的定义以删除对本身。”错误。

我已经完成了我的挖掘工作,每个人似乎都建议取消选中“在所有引用的程序集中重用类型”,但这似乎只在我的项目不使用的服务引用的情况下使用。我确实找到了执行此操作的地方,但看到我没有可引用的服务,我无法配置它。

我对 Visual Studio 不熟悉,所以请放轻松;)

最佳答案

我从来没有设法弄清楚如何回避这一点。我最终做的是将 JSON 中的实体创建为类,然后让 JSON.NET 将 JSON 反序列化为类。

关于visual-studio-2013 - 没有服务引用的项目。错误 : Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35363421/

相关文章:

asp.net-web-api - WebAPI 请求在同一请求上抛出 404 错误...

asp.net - 覆盖 webapi odata 链接的主机

javascript - 为什么添加额外的 header 会导致 AJAX 调用失败

c++ - 运算符优先级更改

visual-studio - 未将对象引用设置为 Visual Studio 2013 上的对象错误实例

c# - 如何将 CheckedListBox 项目设置为默认选中

c++ - 使用 OpenCV 3.0 + Visual Studio 2013 镜像网络摄像头流

c++ - 接口(interface)类型的 CMake 3.0 add_library 中断 get_target_property

c# - 返回新 Task<HttpResponseMessage> 的 IActionFilter 从不返回

asp.net-mvc-4 - Html 表单 - 路径错误