C # Application Console. Read file Json

3

I have a Json file and it always goes wrong when I try to read it. I think because it's an array inside another array and I can not read it.

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
}

Application:

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. '

    
asked by anonymous 31.01.2018 / 13:37

1 answer

3

The map quer property is a list of lists.

In addition, start is an object and not a string , this also needs to be fixed.

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 in GitHub

    
31.01.2018 / 13:42