# json$

Compatible with:
DOS Maximite CMM MM150 MM170 MM+ MMX Picromite ArmiteL4 Armite F4 ArmiteH7 Picomite CMM2

Syntax:
JSON$(array%(),string$)

Description:
JSON$(array%(),string$)
Returns a string representing a specific item out of the JSON input stored in the longstring array%()
The json string to be parsed must be loaded into a LONGSTRING array
e.g.
JSON$(a%(), "name")
JSON$(a%(), "coord.lat")
JSON$(a%(), "weather[0].description")
JSON$(a%(),"list[4].weather[0].description")
Examples taken from api.openweathermap.org

OPTION EXPLICIT
OPTION DEFAULT NONE
DIM MYID$="myID"
DIM APIKEY$="myKEY"
DIM report$="weather"
DIM b$= "GET /data/2.5/"+report$+"?id="+MYID$+"&APPID="+APIKEY$
DIM m%(8000)
b$=b$+" HTTP/1.0"+CHR$(13)+CHR$(10)+CHR$(13)+CHR$(10)
OPEN "com2:115200,8192" AS #1
LONGSTRING CLEAR m%()
PRINT #1,"AT+CWMODE_CUR=1"+CHR$(13)+CHR$(10);
IF NOT OKwait(4000) THEN errorexit("AT+CWMODE_CUR=1")
PRINT #1,"AT+CWJAP_CUR="+CHR$(34)+"mySSID"+CHR$(34)+","+CHR$(34)+"myPASSWORD"+CHR$(34)+CHR$(13)+CHR$(10);
IF NOT OKwait(10000) THEN errorexit("AT+CWJAP_CUR=")
PRINT #1,"AT+CIPSTART="+CHR$(34)+"TCP"+CHR$(34)+","+CHR$(34)+"api.openweathermap.org"+CHR$(34)+",80"+CHR$(13)+CHR$(10);
IF NOT OKwait(10000) THEN errorexit("AT+CIPSTART=")
PRINT #1,"AT+CIPMODE=1"+CHR$(13)+CHR$(10);
IF NOT OKwait(4000) THEN errorexit("AT+CIPMODE=1")
PRINT #1,"AT+CIPSEND"+CHR$(13)+CHR$(10);
IF NOT OKwait(4000) THEN errorexit("AT+CIPSEND")
PRINT #1,b$;
getinput(10000)
PRINT "Weather for "+JSON$(m%(),"name")
PRINT "Temperature is ",VAL(JSON$(m%(),"main.temp"))-273
PRINT "Pressure is ",JSON$(m%(),"main.pressure")
PRINT JSON$(m%(),"weather[0].description")
PRINT #1,"+++";
PAUSE 1000
PRINT #1,"AT+CIPMODE=0"+CHR$(13)+CHR$(10);
IF NOT OKwait(4000) THEN errorexit("AT+CIPMODE=0")
PRINT #1,"AT+CIPCLOSE"+CHR$(13)+CHR$(10);
IF NOT OKwait(4000) THEN errorexit("AT+CIPCLOSE")
END
FUNCTION OKwait(timeout AS INTEGER) AS INTEGER
OKwait=1
TIMER=0
DO
LOOP UNTIL INPUT$(1,1)="O" OR TIMER>=timeout
DO
LOOP UNTIL INPUT$(1,1)="K" OR TIMER>=timeout
IF TIMER>=timeout THEN OKwait=0
END FUNCTION
SUB errorexit(calling AS STRING)
PRINT "AT call timeout from - ",calling
PRINT #1,"AT+CIPCLOSE"+CHR$(13)+CHR$(10)
END
END SUB
SUB getinput(waitpause AS INTEGER)
LOCAL a$
LOCAL j%,i%=-1
TIMER=0
DO
a$=INPUT$(1,1)
IF a$<>"" THEN
LONGSTRING APPEND m%(),a$
' print a$;
IF a$="{" THEN
IF i%=-1 THEN
i%=1
ELSE
i%=i%+1
ENDIF
ENDIF
IF a$="}" THEN i%=i%-1
ENDIF
LOOP WHILE TIMER<=waitpause AND i%<>0
LONGSTRING TRIM m%(),LINSTR(m%(),"{")-1
PRINT ""
END SUB

Last edited: 25 February, 2021