2014-12-07 44 views
0

我一直在閱讀一段時間如何做到這一點,但我從來沒有找到我理解或工作的awnser。Json中的StackOverflowException與Ob​​ject/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」

+1

當然它的意志。它會導致經典的StackoverflowException。重寫它像'public Int32 song_id {get;組; }' – Oybek 2014-12-07 10:39:25

+3

每次引用該屬性時,它會一次又一次地遞歸調用,導致堆棧溢出=) – Oybek 2014-12-07 10:40:08

+0

或者,如果您的意圖是明確指出正文使用字段'private Int32 _song_id; public Int32 song_id {get {return _song_id;} set {_song_id = value}}' – Oybek 2014-12-07 10:41:52

回答

4
public int song_id { get { return song_id; } set { song_id = value; } } 

得到並引用它自己,當你調用它時會導致堆棧溢出。如果你想保存價值的領域嘗試:

private int _song_id; 
public int song_id { get { return _song_id; } set { _song_id = value; } } 

或者只是把它變成:

public int song_id { get; set; } 

喜歡你的其他屬性

+0

現在我們在這裏,我如何獲得像MessageBox一樣的song_id的價值? MessageBox.Show(宋[100] .song_id);? – 2014-12-07 10:47:56

+0

((Song as Song [])[100])。song_id,你的Songs對象來自類型對象。使其鍵入Song []或列表。 – Peter 2014-12-07 10:53:52

+0

我現在有'List 歌曲;'但是當我做'MessageBox.Show(((歌曲爲列表)[100])。song_id);'我得到**附加信息:異常已被目標拋出一個調用。** – 2014-12-07 11:03:30