簡體   English   中英

如何處理Newtonsoft.Json.JsonSerializationException?

[英]How to handle Newtonsoft.Json.JsonSerializationException?

我有一個應該在ListView中顯示返回的數據的表單,該數據以JSON形式顯示:

 [
  {
    "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
 }
]

我嘗試遍歷從服務器返回的結果,但是我似乎遇到了此異常,我之前沒有這個問題,這是我的代碼:

  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();
    }
}
}

這是例外:

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.

抱歉,我無法發表評論-但是我最近想到的有關JSON序列化的事情是,有時要發送的要反序列化的對象是單個項目的形式,用於返回1個數據,或者當更多時,作為數組返回的數據超過1。 (請參閱: 反序列化包含單個或對象數組的JSON

如果是這樣,則可能需要遠離動態。 干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM