简体   繁体   English

Json与C#和Mono

[英]Json with C# and Mono

I'm trying to read a json string into memory and get this undocumented error msg 我正在尝试将json字符串读入内存并获取此未记录的错误msg

$ mcs -r:FortnoxAPILibrary.dll -r:npgsql.dll -r:System.Data.dll -r:Newtonsoft.Json.dll Vouchers.cs 
Vouchers.cs(44,18): error CS0103: The name `JArray' does not exist in the current context
Compilation failed: 1 error(s), 0 warnings

My code is 我的代码是

var json = System.IO.File.ReadAllText("test.json");

var objects = JArray.Parse(json); // parse as array  
foreach(JObject root in objects)
{
    foreach(KeyValuePair<String, JToken> app in root)
    {
        var appName = app.Key;
        var description = (String)app.Value["Description"];
        var value = (String)app.Value["Value"];

        Console.WriteLine(appName);
        Console.WriteLine(description);
        Console.WriteLine(value);
        Console.WriteLine("\n");
    }
}

Where is it documented how this should work? 它在哪里记录该如何工作?

You are more than likely missing a using statement. 您很有可能会丢失using语句。

using Newtonsoft.Json.Linq;

Every piece of C# code you write, except for core types, requires a using statement pointing to any dependencies. 您编写的每个C#代码(核心类型除外)都需要一个using语句,该语句指向任何依赖项。

C# libraries often don't document the using statement requirements for a block of code. C#库通常不记录代码块的using语句要求。 Maybe an oversight, but most users are using an IDE, which warns of the missing statement and offers options to automatically insert them. 也许是疏忽大意,但大多数用户都在使用IDE,该IDE警告缺少的语句并提供自动插入它们的选项。

IDE提示

没有记录,我必须包括这一行。

using Newtonsoft.Json; using Newtonsoft.Json.Linq;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM