在VB.NET上使用Newsoft.Json需要帮助

时间:2018-09-03 14:41:42

标签: c# json.net

我正在尝试使用Newtonsoft.Json,无法使用它。

我是通过NuGet安装的,但是如果我“使用Newtonsoft.Json”放进去,它说Newtonsoft.Json是一个命名空间,不能用作表达式。 谢谢。

Using Newtonsoft.Json;
  JObject channel = JObject.Parse(File.ReadAllText(@"C:\stats.json"));
  channel["points"] = ((number)channel["points"]) += score;
  channel["name"] = ((string)channel["name"]) = name;
End Using

这是JSON:

{
      'stats': {
      'points': 0,
      'name': 'John Doe'
      }
    }

1 个答案:

答案 0 :(得分:0)

您的问题是VB.NET Using语句与具有专用Dispose()方法而不是名称空间的对象一起使用。
例如:

Using DisposableObject As New Object
    DisposableObject.Method1()
End Using

要使用命名空间,您需要做的就是在您的Imports语句中包括Newtonsoft库,如下所示:

Imports Newtonsoft.Json

您可以在模块内的任何地方使用它。

如果要访问整个Newtonsoft库,只需添加Imports Newtonsoft

编辑C#:

在C#中几乎相同,但是用Imports代替using。不需要end using

using Newtonsoft.JSON;
// Place this at the top of your code and ensure the Newtonsoft dll is in the folder with your built app

您还有C#语法错误的小问题:

using (DisposableObject x = new DisposableObject) {
    x.Method1();
}

希望有帮助

相关问题