0

I am generation json which show something like this (var json): ["C:\Users\Admin\Desktop\222.mp3","C:\Users\Admin\Desktop\TGD.mp3"]

How can I parse it with "foreach"? I want to use everysingle link in my app.

    private void Button_Click_7(object sender, RoutedEventArgs e)
    {
        // Eksport
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        saveFileDialog.Filter = "Json (*.json)|*.json";
        if (saveFileDialog.ShowDialog() == true)
        {
            string strResultJson = JsonConvert.SerializeObject(MusicList);
            File.WriteAllText(saveFileDialog.FileName, strResultJson);
            MessageBox.Show("Twoja playlista została zapisana!");
        }
    }

    private void Button_Click_8(object sender, RoutedEventArgs e)
    {
        // Import
        OpenFileDialog fileDialog = new OpenFileDialog
        {
            Multiselect = false,
            DefaultExt = ".json"
        };
        bool? dialogOk = fileDialog.ShowDialog();
        if (dialogOk == true)
        {
            string linkToPlaylist;
            linkToPlaylist = fileDialog.FileName;

            var json = new WebClient().DownloadString(linkToPlaylist);

            // What's now?

            MessageBox.Show(json.ToString());
        }
    }
  • Why are you going to use a WebClient to download a file from your local filesystem? Also, what have you tried? `JsonConvert.DeserializeObject(json)` should do just fine. – CodeCaster May 03 '19 at 11:55
  • WebClient - this is what i found in google and it just worked, so I used it. I just need to somehow loop the results. Can you please tell me more about the JsonConvert.DeserializeObject(json)? How to applicate this? – Kamil Owczarek May 03 '19 at 12:00
  • 1
    Like @CodeCaster already mentioned, you don't need to use WebClient to read text from local file. Or simply use `File.ReadAllText` like this: `var json = JsonConvert.DeserializeObject(File.ReadAllText(linkToPlaylist));` – Luthfi May 03 '19 at 12:00
  • duplicate of [Deserialize JSON with C#](https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – Thierry Prost May 03 '19 at 12:08
  • Now I understand. Thank for help Guys! – Kamil Owczarek May 03 '19 at 12:17

0 Answers0