-2

So Im a bit stumped on how I would save a text file containing JSON data that I am pulling using an api. so Based on my Selections I choose in listbox1 i would need to pull the data for those items and save them in JSON format using a SaveFileDialog. I would also have to load them.

  • Please be more clear about what you want to achieve. Or how you failed to do it so. – Eldar Nov 16 '19 at 21:16
  • well I'm pulling the json data based off of a selection I make from my list box and that json data is being shown via text box and Four different numericupdowns. to be more specific in my listbox I have symbols for different companies stock names and the data that populates into the text box is the company name and the numericupdowns are the displaying different stock prices i.e opening price, closing price , etc .the goal I'm trying to achieve is save that data into a txt file – Carlos Belardo Nov 16 '19 at 21:19
  • You just need to specify how the data ends up in the ListBox/TextBoxes/NumericUpDowns controls. You have a *source*, obviously. The structure of that *source* is the information needed and is what you need to post (if you used a class structure/model, better). – Jimi Nov 16 '19 at 21:29

2 Answers2

0

Assuming you have a string (your JSON) stored in your listbox1 you can get its value, so your JSON, using this:

string JSONstr = libox1.Items.SelectedItem.ToString();

Then if you want to save it in a file you can use:

File.WriteAllText(Filepath, JSONstr);

Filepath is the path you get with the SaveFileDialog

You can then read the file using an OpenFileDialog

Answering your comments:

you can append values to a file using:

File.AppendAllText(Filepath, textbox1.text);
File.AppendAllText(Filepath, textbox2.text);
....
Riccardo Raffini
  • 366
  • 7
  • 20
  • well I'm pulling the json data based off of a selection I make from my list box and that json data is being shown via text box and Four different numericupdowns. to be more specific in my listbox I have symbols for different companies stock names and the data that populates into the text box is the company name and the numericupdowns are the displaying different stock prices i.e opening price, closing price , etc – Carlos Belardo Nov 16 '19 at 21:17
  • So do you need to "get" data form a JSON text and put them in textbox ? – Riccardo Raffini Nov 16 '19 at 21:20
  • Im able to get the data and put that into the text box and the numericupdowns but now I need to save that data into a txt file. – Carlos Belardo Nov 16 '19 at 21:22
  • oh so you need to get all the values from textboxes and save them to a file(?) – Riccardo Raffini Nov 16 '19 at 21:24
0

You can use TransmitFile to download json file

Response.ContentType = "application/json";
Response.AppendHeader("Content-Disposition", "attachment; filename=download.json");
Response.TransmitFile(Server.MapPath("~/mypath/myjson.json"));
Response.End();
junlan
  • 302
  • 1
  • 5