Hack Forums
[.NET] Spliting up a string - 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] Spliting up a string (/showthread.php?tid=4755268)



Spliting up a string - SIM-574702 - Mar 30, 2015

I have some experience in C programming but I'm trying to make an app that uses API to show some data. When request it sent to API it returns all the data in one single line like this

Code
{"mate":{"id":34467414,"name":"Mate","profileIconId":15,"summonerLevel":30,"revisionDate":1424799210000}}

I've got requesting part covered from another tutorial but he just showed whole HTML source of youtube.com in one textbox. What I need is to split received data into 4 string variables, id,name,icon and level where each will contain only data between " ".


* - Hiiro - Mar 30, 2015

Use JSON.net to parse the information.
http://www.newtonsoft.com/json


* - SIM-1676753 - Mar 30, 2015

You can use the DataContractJsonSerializer class.

Example of it's use:
https://gist.github.com/PatPositron/10076559
https://gist.github.com/PatPositron/cc2007bfcffb08708061


* - SIM-574702 - Mar 30, 2015

I'm not looking for something that complicated. I just want to split string on specific places by using basic commands. I'm doing this for myself because I need more examples of working with strings.

This is what I made by following couple of tutorials. I know that I should've created functions instead of having everything under Button_Click but I'm just wondering if this is done properly with using basic commands.

Code
Imports System.Net
Imports System.IO

Public Class Form1
    Private Function getData(ByVal Address As String) As String
      Dim rt As String = ""

      Dim wRequest As WebRequest
      Dim wResponse As WebResponse

      Dim SR As StreamReader

      wRequest = WebRequest.Create(Address)
      wResponse = wRequest.GetResponse

      SR = New StreamReader(wResponse.GetResponseStream)

      rt = SR.ReadToEnd
      SR.Close()

      Return rt
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim temp(20) As String

      TextBox2.Text = getData(TextBox1.Text)

      Dim input As String = getData(TextBox1.Text)
      Dim rawData() As String
      rawData = input.Split(",")

      'Testing array data
      For i = 0 To rawData.Length - 1
      MsgBox(rawData(i))
      Next

      temp = rawData(0).Split(":")
      Dim ID As String = temp(2)
      ID = ID.Replace("""", "")
      MsgBox(ID)

      temp = rawData(1).Split(":")
      Dim name As String = temp(1)
      name = name.Replace("""", "")
      MsgBox(name)

    End Sub

End Class



* - Hiiro - Mar 30, 2015

(Mar 30, 2015 07:06 PM)matemisic Wrote:  I'm not looking for something that complicated. I just want to split string on specific places by using basic commands. I'm doing this for myself because I need more examples of working with strings.

This is what I made by following couple of tutorials. I know that I should've created functions instead of having everything under Button_Click but I'm just wondering if this is done properly with using basic commands.

Code
Imports System.Net
Imports System.IO

Public Class Form1
    Private Function getData(ByVal Address As String) As String
      Dim rt As String = ""

      Dim wRequest As WebRequest
      Dim wResponse As WebResponse

      Dim SR As StreamReader

      wRequest = WebRequest.Create(Address)
      wResponse = wRequest.GetResponse

      SR = New StreamReader(wResponse.GetResponseStream)

      rt = SR.ReadToEnd
      SR.Close()

      Return rt
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim temp(20) As String

      TextBox2.Text = getData(TextBox1.Text)

      Dim input As String = getData(TextBox1.Text)
      Dim rawData() As String
      rawData = input.Split(",")

      'Testing array data
      For i = 0 To rawData.Length - 1
      MsgBox(rawData(i))
      Next

      temp = rawData(0).Split(":")
      Dim ID As String = temp(2)
      ID = ID.Replace("""", "")
      MsgBox(ID)

      temp = rawData(1).Split(":")
      Dim name As String = temp(1)
      name = name.Replace("""", "")
      MsgBox(name)

    End Sub

End Class
Look into Regex if you don't want to handle the JSON with a Deserializer.


* - Ninja - Mar 31, 2015

JSON parsing would be the best option, but you could always do this:
Dim SplitString As String() = Variable.Split("characterhere")
or Regex, which might be a better option.