![]() |
|
[.NET] Grabbing strings from a web server and turning it into an array of strings - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] Grabbing strings from a web server and turning it into an array of strings (/showthread.php?tid=4542373) |
Grabbing strings from a web server and turning it into an array of strings - CatSoup - Nov 17, 2014 How would I go about grabbing an array of strings from a text file on a web server? like: Code { "testing", "testt", "test" }Also how would the text file on the web server need to be formatted? JSON? Is this even possible? I really would like to learn how. I just started learning VB in august. Thank you. * - He B TeMy - Nov 17, 2014 You can use webrequests to read a file, and split it using "," cchar as your example above, i don't know any other way without using a split character. Step at a Step, use this to read: http://stackoverflow.com/questions/2471209/how-to-read-a-file-from-internet It's c#, follow the 2nd upvoted answer. you can use http://converter.telerik.com/ to convert to vb if you need. Then use http://msdn.microsoft.com/en-us/library/system.string.split%28v=vs.110%29.aspx to split. * - Eternal Darkness - Nov 17, 2014 (Nov 17, 2014 03:42 PM)CatSoup Wrote: like: Code Private WebClient As Net.WebClient+1 for liking Blue Exorcist (not adding to your rep, just stating the fact). :) * - Odus - Nov 17, 2014 (Nov 17, 2014 04:25 PM)Eternal Darkness Wrote: -snip That was an example of an array, not the actual format of the page. Even at that, weird way to do it I think! (Nov 17, 2014 03:42 PM)CatSoup Wrote: How would I go about grabbing an array of strings from a text file on a web server? Easy way is to download page as a string. (Nov 17, 2014 03:42 PM)CatSoup Wrote: Could the text file on the web server need to be formatted? JSON? It can be any format you like or it can be plain text, you'll create the array through splitting with whatever you put as your delimiter. * - Eternal Darkness - Nov 17, 2014 (Nov 17, 2014 04:29 PM)Odus Wrote: That was an example of an array, not the actual format of the page. If the page only contains (such as a random plain text file on your server or w/e): (Nov 17, 2014 03:42 PM)CatSoup Wrote: like:The code I posted will delimit it properly and load it into an array that you can sort through... He did not ask how to create a list for his web server, but how to retrieve a list in said format from his web server... If I am understanding correctly that is... (Nov 17, 2014 04:29 PM)Odus Wrote: Even at that, weird way to do it I think! Well, it works... I just threw it together a second ago... Personally I would probably use SubStrings in a loop, gives me a bit more control and will possibly end up being a millionth of a second faster... You mentioned spiting the string with a delimiter, so I thought I would throw it together that way... "The method I suggested is weird"... xD * - CatSoup - Nov 17, 2014 (Nov 17, 2014 04:32 PM)Eternal Darkness Wrote: If the page only contains (such as a random plain text file on your server or w/e): nono. I need something that will grab strings from a file formatted like this: Code testingand then use it in an array like: Code { "testing", "test", "testt" }* - Odus - Nov 17, 2014 (Nov 17, 2014 05:14 PM)CatSoup Wrote: nono. I need something that will grab strings from a file formatted like this: TOLD YOU ETERNAL! Forgive my code, I've spent the last 2 weeks in Swift but you should be good with this: Code Using wc As New WebClient()* - CatSoup - Nov 17, 2014 (Nov 17, 2014 05:19 PM)Odus Wrote: TOLD YOU ETERNAL! Forgive my code, I've spent the last 2 weeks in Swift but you should be good with this: So I have this: Code Imports System.NetHow would I grab a random string from that url and display it into a textbox? * - Odus - Nov 17, 2014 (Nov 17, 2014 05:45 PM)CatSoup Wrote: So I have this: Again, its been a while but here goes: Code Dim R As System.Random = New System.Random()* - NETExplorer - Nov 17, 2014 Disregard everyone above me If you want to do what you are asking correctly, keep on reading Firstly, you'll make life easier on yourself if you change your json a bit. What I mean is make it an actual array, or at least a JSON array Code ["testing","test","tester"]Now that you have that, the way to parse that information in VB, correctly, is using some sort of JSON deserializer http://james.newtonking.com/json That link takes you to the site of an awesome and powerful library that can work with JSON The thing is, it adds an additional dependency at about 500 kb An alternative is to use the DataContractJsonSerializer, which is included in the .NET framework since version 4.0 If you decide to use the DataContractJsonSerializer, I've written a very simple helper class to make it a more flawless experience https://gist.github.com/PatPositron/e6df196d4a76343824ee Just throw that in your project, and you can continue Congratulations, that was the hardest step All you need to do now is get the JSON data from wherever you are supplying it, and pass it to the helper class like so: Code Dim myArrayOfStrings = JsonHelper.Deserialize(Of List(Of String))(myJsonString)It will populate `myArrayOfStrings` with all the data that was in your JSON ------EDIT------ Annnnndddd I just read your reply farther up the thread: (Nov 17, 2014 05:14 PM)CatSoup Wrote: nono. I need something that will grab strings from a file formatted like this: Imma go ahead and leave now, but if you know something of the language you can do what you are looking for using my helper class above 🤝 |