Jump to content

Recommended Posts

Hi, I am trying to get data from a json string, but I get no results when parsing [/script/droids] unless I remove the brackets, but I have to keep them.

#include 'JSON.au3'

Local $sJson = '{"project":"Listing droids","version":"1.0","author":{"name":"Luke","mail":"luke@2080.org"},"[/script/droids]":[{"name":"R2D2","type":"Astromecano","size":"0,96m"},{"name":"BB8","type":"Astromecano","size":"0,67m"},{"name":"C-3PO","type":"Social","size":"1,67m"}]}'

Local $jsonObject = Json_Decode($sJson)

Local $project = Json_Get($jsonObject, '.project')     ; Listing droids
Local $name = Json_Get($jsonObject, '.author.name')    ; Luke
Local $mail = Json_Get($jsonObject, '.author.mail')    ; luke@2080.org
MsgBox(0,'',$project&" - "&$name&" - "&$mail)

Local $aData = Json_Get($jsonObject, '.[/script/droids]')
Local $sName = Json_Get($jsonObject, '.[/script/droids][1].name')
MsgBox(0,'',$sName&" and "&UBound($aData))

 

Link to post
Share on other sites

I am really lost with json.au3 UDF, now I can't encode the data back, [/script/droids] stuff does not appear.

#include "Json.au3"

Local $Obj
Json_Put($Obj, '.project', 'Listing droids')
Json_Put($Obj, '.version', '1.0')
Json_Put($Obj, '.author.name', 'Luke')
Json_Put($Obj, '.author.mail', 'luke@2080.org')

Json_Put($Obj, '.[/script/droids][0].name', 'R2D2')
Json_Put($Obj, '.[/script/droids][0]type', 'Astromecano')
Json_Put($Obj, '.[/script/droids][0]size', '0,96m')

Json_Put($Obj, '.[/script/droids][1].name', 'BB8')
Json_Put($Obj, '.[/script/droids][1].type', 'Astromecano')
Json_Put($Obj, '.[/script/droids][1].size', '0,67m')

Json_Put($Obj, '.[/script/droids][2].name', 'C-3PO')
Json_Put($Obj, '.[/script/droids][2].type', 'Social')
Json_Put($Obj, '.[/script/droids][2].size', '1,67m')

Local $Json = Json_Encode($Obj, $JSON_PRETTY_PRINT)

MsgBox(0,'', $Json)

 

Link to post
Share on other sites
{
   "project": "Listing droids",
   "version": "1.0",
   "author": {
      "name": "Luke",
      "mail": "luke@2080.org"
   },
   "[/script/droids]": [
      {
         "name": "R2D2",
         "type": "Astromecano",
         "size": "0,96m"
      },
      {
         "name": "BB8",
         "type": "Astromecano",
         "size": "0,67m"
      },
      {
         "name": "C-3PO",
         "type": "Social",
         "size": "1,67m"
      }
   ]
}

If you don't mind me asking, what application, API, or site created this JSON?  Or, is this JSON created by one of your own applications?  Although the JSON meets strictly-formatted standards, the key that includes surrounding brackets and slashes ("[/script/droids]") is (to put it tactfully) quite interesting.  There are so many other ways it could have been done that would have kept the original intent.  That particular key appears to be conveying more of a value than a key name.

To help you understand your quandary as it relates to the JSON UDF, if you would have checked @error after your json_puts or json_gets, you would have seen that it failed with @error = 2.  That means that the supplied notation was invalid.  The JSON UDF, that was originally written by Ward and is currently maintained by @Jos, allows for 2 types of notation to identify the path of a value, dot-notation and bracket-notation.  The current implementation of the json_get and json_put functions cannot handle keys that contain a leading bracket ("[").  That's because it conflicts with how the functions identify normal bracket-notations.

Unless or until @Jos, or someone else, modifies the JSON UDF to handle such keys, you will need to find alternative ways to handle JSON datasets with keys that start with "[".  There are other UDFs that can handle strictly-formatted JSON datasets, with keys like the one you've provided.  There are also non JSON-related functions that can probably get the job done as well.  Of course, if you are the one creating that JSON, then I would suggest not having such keys.  Then, your problem with the JSON UDF would no longer be a problem.

Edited by TheXman
Link to post
Share on other sites

As you can see below, the latest version of the json UDF (which was just posted) can correctly handle keys with embedded brackets.

 

 

Link to post
Share on other sites
  • 2 weeks later...

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...