Jump to content

Read Json data


Acce
 Share

Recommended Posts

Hi Im having some trouble working with the JSON.Au3 libary its working find but im a bit sure as how I can extract the data im after 

 

Reading in console (Json_Dump($sJSON)) 

I know I would be intressted in data here -> [33].responseData.city_map.entities[0]

however there are 80 entities how in the world do I extract the data here ?

Also a full line reads like this: 

[33].responseData.city_map.entities[65].__class__  =CityMapEntity 

or

+-> [33].responseData.city_map.entities[66].x  =33

How can I loop through all the data stored here ? 

Earlier it was suggest that I could use something like Json_Get($oJSON, "[33].responseData.city_map.entities[66].x") & @CRLF)

But those numbers are unknown the structure is like this 

+-> [33].responseData.city_map.entities[0].user_id  =11085121
+-> [33].responseData.city_map.entities[0].x  =25
+-> [33].responseData.city_map.entities[0].y  =45
+-> [33].responseData.city_map.entities[0].connected  =1
+-> [33].responseData.city_map.entities[0].state.current_product.product.__class__  =Resources
+-> [33].responseData.city_map.entities[0].state.boosted  =True
+-> [33].responseData.city_map.entities[0].state.is_motivated  =True
+-> [33].responseData.city_map.entities[0].state.__class__  =ProducingState

and after this entities 1 starts 

any suggestion as what I should do or read :)

Questions are how do I know response data is stored as [33] or length for entities ? 

 

Edited by Acce
Link to comment
Share on other sites

  • Developers

Post an example script, with some data in it, that I can run an change and indicate what isn't working. 

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Im working on a code here but this is not the best way to retrieve the data I think 

Here is my work so far all static numbers 

for $i = 0 to 238 
    $id = json_get($object, '[33].responseData.city_map.entities[' & $i & '].id')
    $user_id = json_get($object, '[33].responseData.city_map.entities[' & $i & '].user_id')
    $x = json_get($object, '[33].responseData.city_map.entities[' & $i & '].x')
    $y = json_get($object, '[33].responseData.city_map.entities[' & $i & '].y')
    $connected = json_get($object, '[33].responseData.city_map.entities[' & $i & '].connected')
    $resources_money = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.current_product.product.resources.money')
    
   ConsoleWrite('$id = ' & $id & @CRLF ) ;### Debug Console
   ConsoleWrite('$user_id = ' & $player_id & @CRLF ) ;### Debug Console
   ConsoleWrite('$x = ' & $x & @CRLF ) ;### Debug Console
   ConsoleWrite('$y = ' & $y & @CRLF ) ;### Debug Console
   ConsoleWrite('$connected = ' & $connected & @CRLF ) ;### Debug Console
Next

 

this works but if the server changes the response data from 33 this will break also I noticed  that map.entities does not retun the same data 

 

 

Edited by Acce
messy
Link to comment
Share on other sites

  • Developers

no, just a script with some testdata in it and a few lines of code that tries to retrieve some data that isn't working.

All clear?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Not sure but Its still in testing in my end so I can post my work so far 

 

#include <JSON.au3> ; https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn
#include <File.au3>
HotKeySet("{ESC}", "Terminate")
Local Const $sFilePath = @ScriptDir & "\System\requestId_1.txt"
Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND)
Local $i = 0
    $sJSON = FileReadLine($sFilePath, 1)
Json_Dump($sJSON)
    Local $oJSON = Json_Decode($sJSON)
;ConsoleWrite(Json_Get($oJSON, "responseData.city_map.entities") & @CRLF)
    ;Json_ObjGet($oJSON, ".responseData.city_map.entities")
$object = json_decode($oJSON)
Local $i = 0
    $oJSON = Json_Decode($sJson)
$object = json_decode($sJson)
FileDelete(@ScriptDir & "\System\city_map_entities.txt")
Local Const $city_map_entities = @ScriptDir & "\System\city_map_entities.txt"
Local $hFileOpen = FileOpen($city_map_entities, $FO_APPEND)
    
for $i = 0 to 238
    $id = json_get($object, '[33].responseData.city_map.entities[' & $i & '].id')
    $player_id = json_get($object, '[33].responseData.city_map.entities[' & $i & '].player_id')
    $cityentity_id = json_get($object, '[33].responseData.city_map.entities[' & $i & '].cityentity_id')
    $x = json_get($object, '[33].responseData.city_map.entities[' & $i & '].x')
    $y = json_get($object, '[33].responseData.city_map.entities[' & $i & '].y')
    $connected = json_get($object, '[33].responseData.city_map.entities[' & $i & '].connected')
    $resources_money = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.current_product.product.resources.money')
        $strategy_points = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.current_product.product.resources.strategy_points')
    $product__class__ = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.current_product.product.__class__')
    $product_name = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.current_product.name')
    $production_time = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.current_product.production_time')
    $asset_name = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.current_product.asset_name')
    $product__class = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.current_product.__class__ ')
    $state_boosted = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.boosted')
    $asset_name = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.current_product.asset_name')
    $state_is_motivated = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.is_motivated')
    $next_state_transition_in = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.next_state_transition_in')
    $next_state_transition_at = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.next_state_transition_at')
    $state__class__ = json_get($object, '[33].responseData.city_map.entities[' & $i & '].state.__class__ ')
    $level = json_get($object, '[33].responseData.city_map.entities[' & $i & '].level')
    $__class__ = json_get($object, '[33].responseData.city_map.entities[' & $i & '].__class__')
        FileWrite($hFileOpen, $cityentity_id & @lf)
        ConsoleWrite('$id = ' & $id & @CRLF ) ;### Debug Console
    ConsoleWrite('$player_id = ' & $player_id & @CRLF ) ;### Debug Console
    ConsoleWrite('$cityentity_id = ' & $cityentity_id & @CRLF ) ;### Debug Console
    ConsoleWrite('$x = ' & $x & @CRLF ) ;### Debug Console
    ConsoleWrite('$y = ' & $y & @CRLF ) ;### Debug Console
    ConsoleWrite('$connected = ' & $connected & @CRLF ) ;### Debug Console
    ConsoleWrite('$resources_money = ' & $resources_money & @CRLF ) ;### Debug Console
    ConsoleWrite('$strategy_points = ' & $strategy_points & @CRLF ) ;### Debug Console
    ConsoleWrite('$product__class__ = ' & $product__class__ & @CRLF ) ;### Debug Console
    ConsoleWrite('$product_name = ' & $product_name & @CRLF ) ;### Debug Console
    ConsoleWrite('$production_time = ' & $production_time & @CRLF ) ;### Debug Console
    ConsoleWrite('$asset_name = ' & $asset_name & @CRLF ) ;### Debug Console
    ConsoleWrite('$product__class = ' & $product__class & @CRLF ) ;### Debug Console
    ConsoleWrite('$state_boosted = ' & $state_boosted & @CRLF ) ;### Debug Console
    ConsoleWrite('$asset_name = ' & $asset_name & @CRLF ) ;### Debug Console
    ConsoleWrite('$state_is_motivated = ' & $state_is_motivated & @CRLF ) ;### Debug Console
    ConsoleWrite('$next_state_transition_in = ' & $next_state_transition_in & @CRLF ) ;### Debug Console
    ConsoleWrite('$next_state_transition_at = ' & $next_state_transition_at & @CRLF ) ;### Debug Console
    ConsoleWrite('$state__class__ = ' & $state__class__ & @CRLF ) ;### Debug Console
    ConsoleWrite('$level = ' & $level & @CRLF ) ;### Debug Console
    ConsoleWrite('$__class__ = ' & $__class__ & @CRLF ) ;### Debug Console
Next
FileClose($hFileOpen)
Func Terminate()$read = FileRead($file)
    Exit
EndFunc   ;==>Terminate

 

U would need to download the txt file also 

requestId_1.txt

Link to comment
Share on other sites

  • Developers

Not really what I asked for but it is runable ....  and runs fine after getting rid of the syntax error on the Func Terminate line.
....but it doesn't show me any error....  so what is the issue as this is way to vague for me: ?

12 minutes ago, Acce said:

this works but if the server changes the response data from 33 this will break also I noticed  that map.entities does not retun the same data 

 

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

The issue is this, sorry for confusion.

the for loop uses an static number for end (238) how can I find out the rows in the file ? 

also this file comes from an server , If the server gives a different response next time the number [33] could very well break the script. 

 

Edited by Acce
Link to comment
Share on other sites

4 minutes ago, Acce said:

No its for a database 

The fact that you need the information for a database does not exclude that it is related to a game ;).

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

  • Developers

@Acce,

This is for the game FOE, so please do read our forum rules, which I can only guess you knew already since the JSON DATA contains the word game 233 times. 
Consider this the last warning.

Jos

 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Jos locked this topic
Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...