Skip to content
TORNLIFE More

How to Sort API Array Types

Started by LouBaker [1162207] on in API Development.

9 replies · 113 views · thread synced · 6 days ago · View on torn.com
About this thread

Posts archived: 10 / 10 posts (100%) · the total is Torn's reply count + the opening post at the last fetch

Counted by TornLife from the archived posts.

Archived posts
10
Discussion span
→
People posting
5
Likes on archived posts
2
Authority score
66 / 100
Historical score
21 / 100
Story score
28 / 100
Engagement score
49 / 100
LouBaker [1162207]

I'm having a tough time trying to work out formula for the Array types

I'm working on old VB6 2008 not using any JSON functions.

i can easily work with '{' & '}' because '{' have names like 'happy:{' or 'networth:{'

I use VBs Dictionary function to store the API Tree like Dictionay.Add(key, item)

example:
"stocks":{
"1574420":{
"stock_id":17
"shares":100000
"bought_price":229.5314
"time_bought":1275511646
"time_listed":0
}

When Iv added the keys and items to Dictionary they look like this

example:Dictionary(keys,items)
  • Dictionary("stocks", "")
  • Dictionary("stocks.1574420", "")
  • Dictionary("stocks.1574420.stock_id", "17")
  • Dictionary("stocks.1574420.shares", "100000")
  • ....
I can then reference any of the keys with the command
  • Dictionary.Exist("stocks.1574420.stock_id")
this returns the item


BUT with Array Types there are no Keys to collect only the one

"bazaar":[{
"ID":396
"name":"Business Class Ticket"
"type":"Special"
"quantity":1
"price":25500000
"market_price":8050000
}

{
"ID":206
"name":"Xanax"
"type":"Drug"
"quantity":30
"price":862862
"market_price":860000
}
]

i can't make a reference to bazaar.ID because there would be two under the same Key("bazaar")

example: Dictionary(keys,items)
  • Dictionary("bazaar", "")
  • Dictionary("bazaar.ID", "396")
  • Dictionary("bazaar.name", "Business Class Ticket")
  • Dictionary("bazaar.type", "Special")
  • .....
  • Dictionary("bazaar", "")
  • Dictionary("bazaar.ID", "206")
  • Dictionary("bazaar.name", "Xanax")
  • Dictionary("bazaar.type", "Drug")
  • .....
Does anyone know how can work around this



Plornt [1799359]

I've not used VB before but as I understand you are parsing the JSON data yourself and putting the key/values in a dictionary? I am also guessing the dot notation doesnt do anything special and is just what you've decided to name the individual keys as.

Not entirely certain if there is any array functionality behind VBs dictionaries (although I somewhat doubt it due to the name dictionary usually being synonimous with hashtable) but could you not store array data as keys like so: bazaar[0] bazaar[1] then also have a bazaar.length to be able to loop through. Whilst its not an actual array nor is it pretty it gets around your requirement of just using dictionaries and not using an actual JSON parser.
LouBaker [1162207]

I have tried an internal array, but it doesn't work on how i'm using the data. I should't have to add extra data to the Keys it actually defeat the point of the API

I have a form with Text Objects that have the Key names stored in them.
when the API in read, it does a form search for the Object with the matching Key and Updates it with the new data.

Looping around a large Bazaar with many items and looking for the correct ID and then searching the form for the ID. and some what fiddly. and if i wanted to search for a persific Key doesn't work because i don't know what number in the array where it is.

example:
  • Dictionary.Exist("bazaar.bazaar(?).name")
I dont have any problem sorting out proper arrays as in Honors.

{
"honors_awarded":[1,2,3,6,7,9,11,12,15,16,18,2.......]
}

because they are in the Item as an Array and can be handled.

This is how i would expect the bazaar layout. which think is was like this from start. why it was change i don't know

{
"bazaar":{
396:{
"name":"Business Class Ticket"
"type":"Special"
"quantity":1
"price":25500000
"market_price":8050000
}


It would make much more sense in this format without the array. the data is simple storage and simple to read

example:
  • Dictionary.Exist("bazaar.396.name")
any other ideas how i can fix this.




McNeo [864688]

The two JSON parsers I tried for .NET derped with the Torn API, I think they were mis-reading strings and integers in the earlier days of some things having quotes and others not, so I ended up re-inventing the wheel, and it works great.

I'm doing basically what Lou is describing and saving everything to a matrix - It takes a fraction of a second to parse it all out then I can easily look it up by the index.

I haven't tried any other JSON parsers since then, and I'll probably keep doing it with my home-brewed way until something breaks, which I don't suspect it will.

Lou, what I did to overcome what you're experiencing is to basically save everything in the array like a folder structure. So any key without a value becomes the top level "folder" of the next, if that makes sense. You can see this if you go into the debug screen of my programs, or here, I'll post a screenshot:

[image: s31.postimg.org]

So, when you call the user api the first thing returned is "user" with no value, since everything else is contained within that, so you can see here I simply make that the first "folder" of sorts in the array. Keeping all parameters also prevents any accidental over-writing if you look up another player or something.

To keep your arrays shorter and keep the correct data you need to make sure to search the fields and overwrite a match rather than just adding new entries all the time. Also, for things like item market lookups that not only will change but potentially disappear, you'll want to purge that out of the arrays once you're done using it. There's no easy way to remove array entries, so what I do is simply overwrite the data with "recycle" - then, when something new needs to be written to the array, it checks for any "recycle" fields before writing new indexes.

Hope this helps.
LouBaker [1162207]

I have tried the VB JSON parser. it was buggy and i had to fix it. but when i got running fine. i then found the format for retrieving stuff messy

example:
  • Item = Object.item("stocks").item("1574420").item("stock_id")
I had no idea how to work with this and wasted a load of time trying to find a way to integrate it in to my program. also it was very slow and was hanging my APP's timed events. adding DoEvents made it even slower.

my own way so much simpler
  • Item = Object.Exist("stocks.1574420.stock_id")
I'v parse the API data into a TreeView Object. the TreeView's own Keys, Child KEYs and GrandChild KEYs that hold the KeyData like above.
I have had to leave out Objects within an Arrays (standard Item Array's no problem) out because there isn't any field name to follow.

here's a picture : http://prntscr.com/bnypbz

everything i need is one click away on the TreeView.


Thanks McNeo. I am doing all the stuff you mentioned and more.

user ID's for example are converted into user name's on first time called, and stored in list for selection next time.
every thing is memory cached in Array or stored on disk if the Selection is TORN. for later retrieval. networth is only loaded every 15mins if needed

I have a simple setup for calling APIs
  • api$ = GetAPI("user..profile")
and simple data retrieval
  • dat$ = GetDat("profile.player_id)
everything great except for the Array with Objects

thanks for trying guys

I will think it out in the end

:?
LouBaker [1162207]

the TreeView is where iv come unstuck,
its where keys are help for another part of the app. and easily Selected by the user to AddKeys to the other App.

picture adding key http://prntscr.com/bnzlco

the added Keys go into a Design Window. where it can added as a TEXT Line. in any part of the form, any size, Fonts, Colors and Previous History of the Items Displayed or not

another picture http://prntscr.com/bnzp83

the Idea is the User can contruct there own windows with any information they want.
total up Objects can be added to add or subtract Values read from the Items, and can be added up just like in a spread sheet.

its an ongoing project, will be able to show an finish picture soon.




McNeo [864688]

Okay, interesting.

I think you'd find this sort of object-oriented programming much simpler in VB .NET versus VB6 - it might be worth your time to check out.