C# Console Application. Read Json File

Asked

Viewed 2,980 times

4

I have a Json file and it always gives error when I try to read it. I believe because it is an array within another array and I cannot read.

Json:

{
  "map": [
    ["S", "S", "S", "S"],
    ["S", "S", "C", "S"],
    [ "S", "S", "S", "S" ],
    ["S", "null", "S", "S"]
  ],
  "start": {"X": 3, "Y": 0, "facing": "N"},
  "commands": [ "TL","A","C","A","C","TR","A","C"],
  "battery": 80
}

Applying:

class Program
{
    static void Main(string[] args)
    {
        using (StreamReader r = new StreamReader("../../json1.json"))
        {

            var json = r.ReadToEnd();
            Inicial items = JsonConvert.DeserializeObject<Inicial>(json);
            Console.WriteLine(items.battery);
            Console.ReadKey();

        }
    }

    public class Inicial
    {
        public IList<string> map { get; set; }
        public string start { get; set; }
        public IList<string> commands { get; set; }
        public string battery { get; set; }
    }
}

The error happens on this line:

Inicial items = JsonConvert.DeserializeObject<Inicial>(json);

Error:

Newtonsoft.Json.Jsonreaderexception: 'Unexpected Character encountered while Parsing value: [. Path 'map', line 3, position 5.'

1 answer

4


The estate map is a list of lists.

Besides, start is an object and not a string, this also needs to be corrected.

public class Inicial
{
    public IList<IList<string>> map { get; set; } // Mude aqui
    public string start { get; set; }             // e aqui
    public IList<string> commands { get; set; }
    public string battery { get; set; }
}

public class Start
{
    public int X { get; set; }
    public int Y { get; set; }
    public string faccing { get; set; }
}

Complete code:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Program
{   
    public void Main()
    {
        const string json = @"{
                              ""map"": [
                                [""S"", ""S"", ""S"", ""S""],
                                [""S"", ""S"", ""C"", ""S""],
                                [ ""S"", ""S"", ""S"", ""S"" ],
                                [""S"", ""null"", ""S"", ""S""]
                              ],
                              ""start"": {""X"": 3, ""Y"": 0, ""facing"": ""N""},
                              ""commands"": [ ""TL"",""A"",""C"",""A"",
                                              ""C"",""TR"",""A"",""C""],
                              ""battery"": 80
                            }";

        var items = JsonConvert.DeserializeObject<Inicial>(json);
        Console.WriteLine(items.battery);
    }
}


public class Inicial
{
    public IList<IList<string>> map { get; set; }
    public Start start { get; set; }
    public IList<string> commands { get; set; }
    public string battery { get; set; }
}

public class Start
{
    public int X { get; set; }
    public int Y { get; set; }
    public string faccing { get; set; }
}

See working on . NET Fiddle | Code on the Github

  • Thank you very much! Clarified me and worked perfectly!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.