Jump to content

Help with parsing JSON


B4l4
 Share

Recommended Posts

Hi, I've read all the postings about parsing JSONs but I can't for the life of me get it to work. I'm trying to extract the "name" and the "value". This is what I've done.

#include "Json.au3"
$response='{"raw":{},"class":[{"name":"infer","value":"88.1"}],"ref":"ver1","ex":null,"pr":null,"n":null,"re":null,"res":null}'
$oJSON = Json_Decode($response)
Local $str = json_Get($oJSON, '["class"]["name"]')
ConsoleWrite($str)


Please help! Thanks.

Link to comment
Share on other sites

You were close.  The json_dump() function is always helpful in trying to figure out the correct notation for accessing values.  As it is named, it dumps all of the values with their corresponding, dot notation, accessors.  Below, I slightly modified your code to show you how to use the json_dump() function and how to access your values using both bracket and dot notation.  I hope that helps.

#include "Json.au3"

$response = '{"raw":{},"class":[{"name":"infer","value":"88.1"}],"ref":"ver1","ex":null,"pr":null,"n":null,"re":null,"res":null}'

$oJSON = Json_Decode($response)

; Dump the json values
Json_Dump($response)

; Bracket notation
ConsoleWrite("name  = " & json_Get($oJSON, '["class"][0]["name"]') & @CRLF)
ConsoleWrite("value = " & json_Get($oJSON, '["class"][0]["value"]') & @CRLF)

; Dot notation
ConsoleWrite("name  = " & json_Get($oJSON, '.class[0].name') & @CRLF)
ConsoleWrite("value = " & json_Get($oJSON, '.class[0].value') & @CRLF)

 

Output:

+-> .class[0].name  =infer
+-> .class[0].value  =88.1
+-> .ref  =ver1
+-> .ex  =Null
+-> .pr  =Null
+-> .n  =Null
+-> .re  =Null
+-> .res  =Null
name  = infer
value = 88.1
name  = infer
value = 88.1

 

Edited by TheXman
Link to comment
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
 Share

  • Recently Browsing   0 members

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