Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Spliting up a string

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 " ".

Use JSON.net to parse the information.
http://www.newtonsoft.com/json
Skype: happy.cbb
0x38E6A0912657E
You can use the DataContractJsonSerializer class.

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

[Image: 1zL3pDG.jpg]

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

(Mar 30, 2015 07:06 PM)SIM-574702 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.
Skype: happy.cbb
0x38E6A0912657E
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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by MordiciaNov 15, 2019 07:21 PM 3 211 Nov 15, 2019 07:21 PM
Last Post: Mordicia
[Java] by pureaccaMay 12, 2017 12:57 PM 10 608 May 12, 2017 12:57 PM
Last Post: ComplexEternity
[C#] by goshmulticoloredaltoJul 17, 2016 09:53 AM 2 216 Jul 17, 2016 09:53 AM
Last Post: goshmulticoloredalto