JSON Usage

Stulish

Active Member
Licensed User
Longtime User
Hi guys i am still playing with the JSON library and need some help, i think i am close but i cant see what needs to be done. the little i have created is below:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim RealmData As Map 
   Dim Realms As List  
   Dim EUrealms As Map 
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   Get_Realm_Data
   EUrealms.Initialize 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Get_Realm_Data
Dim RealmStatus As HttpJob, Server As String 

Server = "http://eu.battle.net/api/wow/realm/status"
RealmStatus.Initialize("RealmStatus",Me)
RealmStatus.Download(Server)
End Sub
Sub JobDone (Job As HttpJob)
Dim JSON,JSON1 As JSONParser
If Job.Success = True Then
   Select Job.JobName 
      Case "RealmStatus"
         JSON.Initialize(Job.GetString)
         Log(Job.GetString)
         RealmData = JSON.NextObject 
         Realms = RealmData.get("realms")
         Log(RealmData)
         Log(Realms)
         For a=0 To Realms.Size-1
            EUrealms.Put(a,Realms.Get(a))
         Next
         Log(EUrealms.Get(1))
         Log(Realms.Size)
   End Select
End If
Job.Release 
End Sub

i can get each realm into a list but then cant get the information in the list unless i use substring looking for the field i want, i believe i should be able to get each realm into a different map. for this server there are 266 realms, i have used JSON Online Viewer to look at the data just open the link and click on 'Load JSON Data' and paste http://eu.battle.net/api/wow/realm/status into the box, and you can see the data.

This viewer puts the data it realm, i created a map and tried to put the data into realms map but i had an error as is said i can not use a map, so i changed it to a list and this allowed the data to be inserted, but it isn't much use,

the JSON Structure can be seen in the image i have attached, there are 266 realms following the same structure format.

Does anyone have a way forward.

Thanks

Stu
 

Attachments

  • JSON Structure.png
    JSON Structure.png
    15.6 KB · Views: 301

Stulish

Active Member
Licensed User
Longtime User
Thanks Erel,

That worked a treat, i have now tried to implement what you said above to put all of the data into user defined types with limited success.

the code is below that i am using (it is also attached as a zip):

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim RealmData As Map 
   Dim Realms As List  
   Dim EUrealms As Map 
   Type pvpArea(status As Int, ne As Int, faction As Int, area As Int)
   Type RealmInfo(wintergrasp As pvpArea, queue As Boolean, timezone As String, battlegroup As String, status As Boolean, locale As String, name As String, slug As String, tolbarad As pvparea, pvp As String, population As String)
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   Get_Realm_Data
   EUrealms.Initialize 
End Sub
Sub Activity_Resume

End Sub
Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Get_Realm_Data
Dim RealmStatus As HttpJob, Server As String 
Server = "http://eu.battle.net/api/wow/realm/status"
RealmStatus.Initialize("RealmStatus",Me)
RealmStatus.Download(Server)
End Sub
Sub JobDone (Job As HttpJob)
Dim a As Int, JSON,JSON1 As JSONParser,winter, tolb As Map, Ri As RealmInfo, Win, tol As pvpArea 
If Job.Success = True Then
   Select Job.JobName 
      Case "RealmStatus"
         JSON.Initialize(Job.GetString)
         Log(Job.GetString)
         RealmData = JSON.NextObject
         Realms = RealmData.Get("realms")
         a=0
         winter.Initialize 
         tolb.Initialize 
         Win.Initialize 
         tolb.Initialize 
         EUrealms.Initialize 
         For Each Realm As Map In Realms
            winter.Clear 
            tolb.Clear 
            'Log(Realm.Get("wintergrasp"))
            winter = Realm.Get("wintergrasp")
            tolb = Realm.Get("tol-barad")
            Win.status = winter.Get("status")
            Win.ne = winter.Get("next")
            Win.faction = winter.Get("controlling-faction")
            Win.area = winter.Get("area")
            tol.status = tolb.Get("status")
            tol.ne = tolb.Get("next")
            tol.faction = tolb.Get("controlling-faction")
            tol.area = tolb.Get("area")
            Ri.wintergrasp = Win
            Ri.queue = Realm.Get("queue")
            Ri.timezone = Realm.Get("timezone")
            Ri.battlegroup = Realm.Get("battlegroup")
            Ri.status = Realm.Get("status")
            Ri.locale = Realm.Get("locale")
            Ri.name = Realm.Get("name")
            Ri.slug = Realm.Get("slug")
            Ri.tolbarad = tol
            Ri.pvp = Realm.Get("type")
            Ri.population = Realm.Get("population")
            EUrealms.Put(a,Ri)
            'Log(EUrealms.GetValueAt(a))
            a=a+1
         Next
   End Select
   For a=0 To EUrealms.Size-1
      Log(a & ":" & EUrealms.GetValueAt(a))
   Next
End If
Job.Release 
End Sub

if i add in the Log(EUrealms.GetValueAt(a)) that is commented out i get each item returned correctly. but if i iterate over all of the element the last received item is in all elements i can not see where i have messed up, basically all 266 element contain the same data.

Any help is appreciated.

Thanks

Stu
 

Attachments

  • WOWRealmCheck.zip
    8.9 KB · Views: 240
Upvote 0

Stulish

Active Member
Licensed User
Longtime User
Thanks Erel,

That worked, i thought if i dim a variable each time the loop iterated through it would just over wright the variable and then save the new variable in the map.

Regards

Stu
 
Upvote 0
Top