Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26647

Json with C# and Mono

I'm trying to read a json string into memory and get this undocumented error 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?

Upvotes: 0

Views: 854

Answers (2)

Tom Bowers
Tom Bowers

Reputation: 5140

You are more than likely missing a using statement.

using Newtonsoft.Json.Linq;

Every piece of C# code you write, except for core types, requires a using statement pointing to any dependencies.

C# libraries often don't document the using statement requirements for a block of code. Maybe an oversight, but most users are using an IDE, which warns of the missing statement and offers options to automatically insert them.

IDE Hints

Upvotes: 1

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26647

It is not documented that I must include this line.

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

Upvotes: 0

Related Questions