[WANTED] vb.net, json & php example

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
7 posts Page 1 of 1
Contributors
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

I need an example of how to use
json in a vb.net windows form.
using data string from a php file.

the code i have been using does not work. at all!
no errors, just a simple nothing.

to help you out,
1) using just a simple listbox to list the json string data.
2) a class library to hold the json markers
3) php using either mysql, or hard coded string in the php file itself.

any help would be great.
Image
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Do you have an example of the actual JSON string that's returned by the PHP script? It would help a lot to get code working with it :)
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

This is a basic example of how you can handle json data. You will need to add a reference to System.Web.Extensions

This is the php file info for output, it just outputs a list of names with age and sex:
Code: Select all
<?php 

		$data = array(
    		(object)array('Name' => 'Bob','Sex' => 'Male','Age' => '56',),
    		(object)array('Name' => 'Jeff','Sex' => 'Male','Age' => '14',),
			(object)array('Name' => 'Sue','Sex' => 'Female','Age' => '22',),
		);
			
    	echo json_encode($data);
 
?> 
Then in the vb form code I just created a class to hold the data and using a button and a listbox to read the php json data and display it inside a listbox, this is the entire code:
Code: Select all
Imports System.Web.Script.Serialization

Public Class Form1

    'Json data class
    Public Class People
        Public Name As String
        Public Sex As String
        Public Age As String
    End Class

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim list As List(Of People)
        Try
            Dim webClient As New System.Net.WebClient
            Dim result As String = webClient.DownloadString("http://localhost/jsontest.php")
            webClient.Dispose()

            list = New JavaScriptSerializer().Deserialize(result, GetType(List(Of People)))

            For Each item As People In list
                ListBox1.Items.Add(item.Name & " " & item.Age & " " & item.Sex)

            Next

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub
End Class
Obviously change the URL to wherever the php file is located.

Hope that helps cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

CodenStuff wrote:
This is a basic example of how you can handle json data. You will need to add a reference to System.Web.Extensions
This is the php file info for output, it just outputs a list of names with age and sex:
Code: Select all
<?php 
		$data = array(
    		(object)array('Name' => 'Bob','Sex' => 'Male','Age' => '56',),
    		(object)array('Name' => 'Jeff','Sex' => 'Male','Age' => '14',),
			(object)array('Name' => 'Sue','Sex' => 'Female','Age' => '22',),
		);	
    	echo json_encode($data);
?> 
Then in the vb form code I just created a class to hold the data and using a button and a listbox to read the php json data and display it inside a listbox, this is the entire code:
Code: Select all
Imports System.Web.Script.Serialization
Public Class Form1

    'Json data class
    Public Class People
        Public Name As String
        Public Sex As String
        Public Age As String
    End Class

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim list As List(Of People)
        Try
            Dim webClient As New System.Net.WebClient
            Dim result As String = webClient.DownloadString("http://localhost/jsontest.php")
            webClient.Dispose()
            list = New JavaScriptSerializer().Deserialize(result, GetType(List(Of People)))
            For Each item As People In list
                ListBox1.Items.Add(item.Name & " " & item.Age & " " & item.Sex)
            Next
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class
Obviously change the URL to wherever the php file is located.
Hope that helps cooll;


ok this work perfect!
but i was wondering about the php part.
is there a way to do like a multi level array()


(object)array('Name' => 'Bob','Sex' => 'Male','Age' => '56',)


the raw jason
Code: Select all
[{"Name":"Bob","Sex":"Male","Age":"56"},
{"Name":"Jeff","Sex":"Male","Age":"14"},
{"Name":"Sue","Sex":"Female","Age":"22"}]
but i would like to know if i could do something like this.
Code: Select all
{"menu": {
  "id": "file",
  "value": "File",
  "popup": {
    "menuitem": [
      {"value": "New", "onclick": "CreateNewDoc()"},
      {"value": "Open", "onclick": "OpenDoc()"},
      {"value": "Close", "onclick": "CloseDoc()"}
    ]
  }
}}
Image
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

lol that's where it starts getting annoying for me, I'm sure someone will know a better way but here's a quick mock-up as example:

php file info:
Code: Select all
<?php 
		
		$output = array();
		$menu = array();
		$data = array();
		
		//Create the main output
		$data["id"] = 'file';
   		$data["value"] = 'File';
		
		//Create list of menu items
		$menu = array(
    		(object)array('value' => 'New','onclick' => 'CreateNewDoc()',),
    		(object)array('value' => 'Open','onclick' => 'OpenDoc()',),
			(object)array('value' => 'Close','onclick' => 'CloseDoc()',),
		);
		
		//Add list of menu items to main output
  		$data["menuitem"] = $menu;
			
		//Add everything to single output
		array_push($output, $data);
    		
		//Output lol
     	echo json_encode($output);
		
?> 
The vb code using a button and a menustrip:
Code: Select all
Imports System.Web.Script.Serialization

Public Class Form1

    'Json data class
    Public Class People
        Public id As String
        Public value As String
        Public menuitem As List(Of Menuitems)
    End Class

    Public Class Menuitems
        Public value As String
        Public onclick As String
    End Class

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim list As List(Of People)
        Try
            Dim webClient As New System.Net.WebClient
            Dim result As String = webClient.DownloadString("http://localhost/jsontest.php")
            webClient.Dispose()

            list = New JavaScriptSerializer().Deserialize(result, GetType(List(Of People)))

            For Each item As People In list
                Dim mnu = New ToolStripMenuItem(item.value)
                For Each itm As Menuitems In item.menuitem
                    mnu.DropDownItems.Add(itm.value)
                Next
                MenuStrip1.Items.Add(mnu)
            Next
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub
End Class
It works at least lol ... dunnno;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

How about we try to keep this a little simple.

what my code does is makes a request to the api i am writing.
the request only has an id number sent. ("~\api.php?sec=72")

now the json should return back something like this:
Code: Select all

sector:
   id = 72:
   nav = 100, 433, 287, 16, 83 'random generated navigation links. i have this code
   'then here is the sector data (planets, ports, mines, fighters, etc...
   planet: id, str_1, str_2, str_3,
   port:  id, str_1, str_2, str_3,
   beacon: id, str_1, str_2, str_3,
   mine: id, str_1, str_2, str_3,
   fighter: id, str_1, str_2, str_3,


when the "nav" link is clicked, it sends the request to the api
and the api will gather all the data it needs to send to the player.


something along those lines anyway.
Image
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

It looks like you want to output quite a lot of data for loads of different things and I'm not sure if there's a quick simple way of doing that..I could be wrong :?

You should do a google search for outputting mysql to json, like this basic blog post:
http://www.webinfopedia.com/php-export- ... -json.html

In my experience creating the site API I had to do quite a few queries and loops to retrieve, gather and merge all the data in to readable json output.

Hopefully comathi or someone else may know of a way cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
7 posts Page 1 of 1
Return to “Tutorial Requests”