我一直在閱讀一段時間如何做到這一點,但我從來沒有找到我理解或工作的awnser。Json中的StackOverflowException與Object/String []
這是我的代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Utilities;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
namespace SongTunes
{
public partial class Form1 : Form
{
Object Songs;
public class Song
{
public int song_id { get { return song_id; } set { song_id = value; } }
public string sorting_number { get; set; }
public string name { get; set; }
public string description { get; set; }
public string lyrics { get; set; }
public string purchase_link { get; set; }
public string youtube_id { get; set; }
public string has_music_video { get; set; }
public string allow_downloads { get; set; }
public string raw_artist_id { get; set; }
public string artist_id { get; set; }
public string album_artist_id { get; set; }
public string release_date { get; set; }
public string artist_name { get; set; }
public string album_artist_name { get; set; }
public string album { get; set; }
public string bitrate { get; set; }
public string duration { get; set; }
public string filesize { get; set; }
public string path { get; set; }
public string is_part_of_compilation { get; set; }
public string visible { get; set; }
public string track_number { get; set; }
public string is_explicit { get; set; }
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button1.Text = "Loading...";
backgroundWorker1.RunWorkerAsync();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void backgroundWorker1_Done(object sender, RunWorkerCompletedEventArgs e)
{
button1.Enabled = true;
button1.Text = "reload";
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string address = "";
System.Net.WebClient webclient = new WebClient();
string json = webclient.DownloadString(address);
Songs = JsonConvert.DeserializeObject<List<Song>>(json);
}
private void SongList_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
這只是第一
public int song_id { get { return song_id; } set { song_id = value; } }
崩潰與 「類型 'System.StackOverflowException' 未處理的異常發生在SongTunes.exe」
當然它的意志。它會導致經典的StackoverflowException。重寫它像'public Int32 song_id {get;組; }' – Oybek 2014-12-07 10:39:25
每次引用該屬性時,它會一次又一次地遞歸調用,導致堆棧溢出=) – Oybek 2014-12-07 10:40:08
或者,如果您的意圖是明確指出正文使用字段'private Int32 _song_id; public Int32 song_id {get {return _song_id;} set {_song_id = value}}' – Oybek 2014-12-07 10:41:52