Click here to Skip to main content
15,905,912 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello Friend I am working on a JSON API and the output is something like this

C#
{
"players":
[
{
"name":"1ApRiL",
"rank":13,
"rank_name":"STAFF SERGEANT I",
"veteran":4,
"score":195385,
"level":164,
"kills":821,
"deaths":736,
"time":49090.766,
"elo":118.936,
"form":1,
"date_lastupdate":"2010-03-21T02:02:49+01:00",
"count_updates":17,
"general":{},
......
},
......
]

"requested":2,
"found":0
}


But can anyone tell me how to manage each value in each label and how to do it?
Posted
Comments
Prasad Khandekar 30-May-13 11:28am    
What you want to do?
ToxicF3AR 30-May-13 11:32am    
Like in lable 1 it will show the name
and in label 2 it will show the rank
like this can we do it and is it possible ?
Prasad Khandekar 30-May-13 12:17pm    
You can use a JSON Library such as DynamicJson or JSON.Toolkit to achieve this. Basically by using these libraries you will get a reference to dynamic JSON Object and then from your code behind you can extract individual named items and assign those to your controls.
ToxicF3AR 30-May-13 13:17pm    
Any Example will be better for me
Sergey Alexandrovich Kryukov 30-May-13 12:20pm    
And what do you mean by "VB", exactly?
—SA

You ca read the json using jQuery and dynamically create labels for that.

See the DEMO - [DEMO] Working With JSON API[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-May-13 15:17pm    
Nice, a 5.
—SA
Thanks Sergey Alexandrovich Kryukov... :)
Thanks you guys but I have fixed it

VB
Imports System.Net
Imports System.IO

Module Module1

    Sub Main()

        Console.WriteLine("I love Qmz_")
        Console.WriteLine("Press any key to start")
        Console.ReadKey()
        Console.WriteLine(vbNewLine)
        Console.WriteLine("Checking... Please wait.")
        Console.WriteLine(vbNewLine)
        Dim url As String = "your api link here"
        Dim json As String = String.Empty

        Using client As New WebClient
            Try
                json = client.DownloadString(url)
            Catch ex As Exception
                MsgBox(ex.Message)
                Exit Sub
            End Try
        End Using

        json = json.Replace(vbLf.ToCharArray, "")
        json = json.Replace(Chr(34), "")

        Dim pos As Integer = InStr(json, "players")

        json = json.Substring(pos - 1, Len(json) - pos)
        pos = json.IndexOf("}")
        json = json.Substring(0, pos)

        Dim jsonArray() As String = json.Split(",".ToCharArray)
        Dim temp() As String

        For i As Integer = 0 To jsonArray.GetUpperBound(0)

            temp = jsonArray(i).Split(":".ToCharArray)

            If jsonArray(i).IndexOf("players") > -1 Then
                Console.WriteLine("Players: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("name") > -1 Then
                Console.WriteLine("name: " & temp(1).Trim())
            ElseIf jsonArray(i).IndexOf("rank") > -1 Then
                Console.WriteLine("rank: " & temp(1).Trim())
            ElseIf jsonArray(i).IndexOf("rank_name") > -1 Then
                Console.WriteLine("rank name: " & temp(1).Trim())
            ElseIf jsonArray(i).IndexOf("veteran") > -1 Then
                Console.WriteLine("veteran: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("score") > -1 Then
                Console.WriteLine("score: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("level") > -1 Then
                Console.WriteLine("level: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("kills") > -1 Then
                Console.WriteLine("kills: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("deaths") > -1 Then
                Console.WriteLine("deaths: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("time") > -1 Then
                Console.WriteLine("time: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("elo") > -1 Then
                Console.WriteLine("elo: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("form") > -1 Then
                Console.WriteLine("form: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("date_lastupdate") > -1 Then
                Console.WriteLine("last update date: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("count_update") > -1 Then
                Console.WriteLine("count update: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("general") > -1 Then
                Console.WriteLine("general: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("requested") > -1 Then
                Console.WriteLine("requested: " & temp(1).Trim)
            ElseIf jsonArray(i).IndexOf("found") > -1 Then
                Console.WriteLine("found: " & temp(1).Trim)

            End If

            Erase temp

        Next

        Erase jsonArray
        Console.WriteLine(vbNewLine)
        Console.WriteLine("Done !")
        Console.ReadKey()
    End Sub

End Module
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900