1

hei guys, i'm having this error while debugging my app, i'm trying to download data from an sql database formatting it in a json string, i want to create an array of elements (A) that contains his informations (B1)(B2)(B3)(B4)..

array a1 = {b1,b2,b3} array a2 = {b1,b2,b3} and so on..

but this code doesn't work, i have this error:

Newtonsoft.json.jsonreaderexeption has been thrown error parsing boolean value. path 'piatti[0].disponibilita',line 9, position 21

i'm sorry for my bad english

public class piatto //NOME DELL'ELEMENTO RIPETUTO
        {
            public List<piatto_details> piatti { get; set; }
        }

        public class piatto_details //DETTAGLI DELL'ELEMENTO
        {
            public string categoria { get; set;}
            public string nome { get; set; }
            public string prezzo { get; set; }
            public string dettagli { get; set; }
            public string img_url { get; set; }

            public bool disponibile { get; set; }
            public bool gluten_free { get; set; }

        }

        public string[,] GetTable(string url, int details)
        {
            string[,] lista_piatti;
            lista_piatti = new string[,] { };

            WebClient client = new WebClient();

            string value = client.DownloadString(url);
            Console.WriteLine(value);

            piatto piatt = JsonConvert.DeserializeObject<piatto>(value);

            int i = 0;
            int j = 0;

            foreach (var item in piatt.piatti)
                {
                    for (j = 0; j < details; j++)
                    {
                        switch (j)
                        {
                            case 0:
                            lista_piatti[i, j] = item.categoria;
                            break;
                            case 1:
                            lista_piatti[i, j] = item.nome;
                            break;
                            case 2:
                            lista_piatti[i, j] = item.prezzo;
                            break;
                            case 3:
                            lista_piatti[i, j] = item.dettagli;
                            break;
                            case 4:
                            lista_piatti[i, j] = item.img_url;
                            break;
                            case 5:
                            lista_piatti[i, j] = Convert.ToString(item.disponibile);
                            break;
                            case 6:
                            lista_piatti[i, j] = Convert.ToString(item.gluten_free);
                            break;
                            default:
                            Console.WriteLine("Error while switching details");
                            break;
                        }
                    }
                    j = 0;
                    i++;
                }

            return lista_piatti;

        }

Here is the consolle log printing 'value'

'piatti':[
    {
        'categoria':'primi',
        'nome':'pasta al bue',
        'prezzo':'26',
        'dettagli': 'il bue si chiamava rosario il bue dromedario',
        'img_url': 'http://www.google.it',
        'disponibilita': true;
        'gluten_free' : 'false';

     },
      {
        'categoria':'primi',
        'nome':'pasta alla gallina',
        'prezzo':'21',
        'dettagli': 'la gallina rosita, morta purtroppo',
        'img_url': 'http://www.google.it',
        'disponibilita': false;
        'gluten_free' : 'true';

     },
      {
        'categoria':'secondi',
        'nome':'gallina rosita',
        'prezzo':'210',
        'dettagli': 'l'altro pezzo della gallina rosita',
        'img_url': 'http://www.google.it',
        'disponibilita': true;
        'gluten_free' : 'true';

     }

    ] 
 }
Andrea Loda
  • 71
  • 11

2 Answers2

1

Your problem may be that 'gluten_free' : 'false' is returning a string value, where a boolean is expected...so should be 'gluten_free' : false instead...try using a stronger deserializer like the microsoft one here: https://msdn.microsoft.com/en-us/library/tz8csy73(v=vs.110).aspx and if that doesn't work you can try switching the value manually on the return

probably easiest thing to do is turn public bool gluten_free { get; set; } to public string gluten_free { get; set; } especially since this is happening for everyone of your objects

Adam Weitzman
  • 1,552
  • 6
  • 23
  • 45
0

The problem is in your JSON, it's not well formatted.

Here's what's wrong:

'img_url': 'http://www.google.it',
'disponibilita': true;
'gluten_free' : 'false';

There's a Semicolon after "true" at "disponibilita". That should be a normal Comma (Making it a Property from the object, just like all other properties).

Like Adam pointed out, there's also a difference between your true and 'false' (with and without quotes) in the JSON. Although I would prefer it without quotes, I think the serialiser would parse this correctly (string to boolean conversion).

Bottomline, the JSON output is wrong. You can validate it by using a JSON validator like this one. Good luck!

Community
  • 1
  • 1
MarcoK
  • 6,090
  • 2
  • 28
  • 40