robertoal

Using a JSON decoder

2 posts in this topic

Dear folks.

I have a question about JSON, I tried different library's but none of them seem te work. 

My goal:

JSON string to array

 

Library's tried:

Well everyone I could find.. I think the one from WARD is best, but hell. I am an amateur. It is the only one notting spitting out random errors, hence my choice.

 

My code thus far:

 

#include <Array.au3>
#Include "Json.au3"


$URL_Activities = 'xxx'
$URL_Vehicles = 'xxx'


$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("GET",$URL_Vehicles, False)
$oHTTP.setRequestHeader("postman-token", "xxx")
$oHTTP.setRequestHeader("cache-control", "no-cache")
$oHTTP.setRequestHeader("authorization", "Basic xxx")

$oHTTP.Send()
$oReceived = $oHTTP.ResponseText
$oStatusCode = $oHTTP.Status

ConsoleWrite($oReceived & @crlf)
ConsoleWriteError("StatusCode: - " & $oStatusCode & @crlf)


Local $Json1 = '[{"access_token":"25f64a9e27b7903eacdff8e36fd74c80","vehicles":[{"id":5649,"name":"Ebike6","petrol_usage_per_kilometer":null,"petrol_type":null,"frequency_usage_per_week":null,"co2_emission_per_km":null,"travel_time_per_day":null,"travel_distance_per_day":null,"type":{"id":3,"name":"Bike","combustion":false},"deleted_at":null},{"id":5650,"name":"Ebike7","petrol_usage_per_kilometer":null,"petrol_type":null,"frequency_usage_per_week":null,"co2_emission_per_km":null,"travel_time_per_day":null,"travel_distance_per_day":null,"type":{"id":3,"name":"Bike","combustion":false},"deleted_at":null},{"id":5651,"name":"Ebike8","petrol_usage_per_kilometer":null,"petrol_type":null,"frequency_usage_per_week":null,"co2_emission_per_km":null,"travel_time_per_day":null,"travel_distance_per_day":null,"type":{"id":3,"name":"Bike","combustion":false},"deleted_at":null},{"id":5652,"name":"Ebike9","petrol_usage_per_kilometer":null,"petrol_type":null,"frequency_usage_per_week":null,"co2_emission_per_km":null,"travel_time_per_day":null,"travel_distance_per_day":null,"type":{"id":3,"name":"Bike","combustion":false},"deleted_at":null},{"id":5657,"name":"Default Car","petrol_usage_per_kilometer":0.07142857142857142,"petrol_type":"gasoline","frequency_usage_per_week":5,"co2_emission_per_km":171,"travel_time_per_day":20,"travel_distance_per_day":15,"type":{"id":1,"name":"Car","combustion":true},"deleted_at":null},{"id":5658,"name":"Default Bike","petrol_usage_per_kilometer":null,"petrol_type":null,"frequency_usage_per_week":null,"co2_emission_per_km":null,"travel_time_per_day":null,"travel_distance_per_day":null,"type":{"id":3,"name":"Bike","combustion":false},"deleted_at":null}]}]'

Local $Data1 = Json_Decode($Json1,True)
_ArrayDisplay($Data1,"Test")

 

The $Json1 VAR is the json I get from the http object, if i test this with "Json formatter" it looks like proper JSON.

Bet I get nothing back from the Json_decode function. 

I am running out of ideas, anybody can shed some light on this?

 

Thanks in advance.

BinaryCall.au3

Json.au3

Json_Test.au3

test.json

Share this post


Link to post
Share on other sites



I usually just use 

$oReceived = StringTrimLeft(StringTrimRight($oReceived, 2), 2) ;~ Trims 2 characters off each end of the received json string. This removes [{ and }] and makes it look cleaner.
$aJsonReply = StringSplit($oReceived, ":,") ;~ Turn the modified received json string into an array. We can ubound to find how many entries it has.

Additionally, if you #include something in your example code, you should make those includes visible in your original post. Otherwise, in order to help you, we have to download the 2 #include files and load them into AutoIt just to see how they function. For those willing to help, it is a massive inconvenience. 


False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for fast and easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

SoundBoard - Play any song or sound you want at the press of a hotkey.

My GitHub Page: https://github.com/BetaLeaf

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now