0

I have a form that's supposed to display the returned data in a ListView, the data in the form of JSON as such:

 [
  {
    "intel_content": "This intel is top secret.", 
    "intel_date": "2017-07-01T22:36:57.353345+00:00", 
    "intel_id": 3, 
    "intel_title": "2001 Intel", 
    "operation_id": 1, 
    "source_id": 1
 }, 
 {
    "intel_content": "This is another intel on the first operation. ", 
    "intel_date": "2017-07-01T22:35:35.720128+00:00", 
    "intel_id": 2, 
    "intel_title": "Intel 505", 
    "operation_id": 1, 
    "source_id": 1
 }
]

I try to loop through the result that get back from the server but I seem to encounter this exception, I didn't have this problem earlier This is my code:

  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using MetroFramework.Forms;
  using MetroFramework;
  using DalSoft.RestClient;
  using DalSoft.RestClient.Handlers;
  using System.Net;
  using Newtonsoft.Json;

 namespace IGIS_Committee
 {
public partial class Dashboard : MetroForm
{
    public Dashboard()
    {
        InitializeComponent();
    }
    public static long selectedItl = -1;

    private void Dashboard_Load(object sender, EventArgs e)
    {
        listIntels.Items.Clear();
        String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(Login.username + ":"+ Login.password));
        dynamic client = new RestClient("http://localhost:5000/api/v1/", new Dictionary<string, string> { { "Authorization", "Basic " + encoded } });
        getIntels(client);
    }
    public async void getIntels(dynamic client)
    {
        var result = await client.intels.Get();

        //dynamic dynamicObject = JsonConvert.DeserializeObject(result.ToString());

        // This is where the exception occurs

        foreach (var it in result)
        {
            ListViewItem itl = new ListViewItem(it.intel_id.ToString());
            itl.SubItems.Add(it.intel_title);
            itl.SubItems.Add(it.source_id.ToString());
            itl.SubItems.Add(it.intel_date.ToShortDateString());
            itl.SubItems.Add(it.operation_id.ToString());
            itl.SubItems.Add(it.intel_content);
            listIntels.Items.Add(itl);
        }
    }

    private void listIntels_DoubleClick(object sender, EventArgs e)
    {
        selectedItl = long.Parse(listIntels.SelectedItems[0].SubItems[0].Text);
        IntelDetail fm = new IntelDetail();
        fm.ShowDialog();
    }

    private void tlPost_Click(object sender, EventArgs e)
    {
        RateIntel fm = new RateIntel();
        fm.Show();
    }

    private void tlViewIntels_Click(object sender, EventArgs e)
    {
        CommitteeRatings fm = new CommitteeRatings();
        fm.Show();
    }

    private void tileLogout_Click(object sender, EventArgs e)
    {
        this.Close();
        Login fm = new Login();
        fm.Show();
    }
}
}

This is the exception:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current 
JSON object (e.g. {"name":"value"}) into type 
'System.Collections.IEnumerable' because the type requires a JSON array 
(e.g. [1,2,3]) to deserialize correctly.
hylo
  • 43
  • 6
  • 1
    Please post the whole exception. – stelioslogothetis Jul 02 '17 at 07:32
  • Ideally you should create a class that corresponds to the data you're receiving instead of using dynamic. Then deserialize your JSON into a `List`. Using dynamic really complicates things, so unless it is completely necessary, remove it. – stelioslogothetis Jul 02 '17 at 07:41
  • I have used dynamic throughout my project, It would be nice If I hadn't to change everything, I have at least another 10 forms like that @stybl – hylo Jul 02 '17 at 07:49
  • The way you have written it, the program will most likely not even work properly. Unless a cast is involved in which case you'd need that class anyway. My recommendation is to switch. Not only will it solve your problem, but it is in accordance with best practice. – stelioslogothetis Jul 02 '17 at 07:52
  • You have wanted a dynamic swamp. Enjoy now! – Alexander Petrov Jul 02 '17 at 08:11

1 Answers1

0

Sorry, I'm unable to comment -- but something that comes to my mind about JSON serializing recently is that sometime the object that was sent to be deserialized is in the form of a single item for 1 data returned OR as an array when more than 1 data returned. (Refer:Deserializing JSON containing single or array of objects)

If it is, you may need to move away from dynamic. Cheers

hsoesanto
  • 513
  • 2
  • 9