Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't access array after parse #94

Open
mvlaran opened this issue Jul 10, 2020 · 4 comments
Open

Can't access array after parse #94

mvlaran opened this issue Jul 10, 2020 · 4 comments
Labels
bug fix-on-development-branch A fix for this issue was implemented on current development branch.

Comments

@mvlaran
Copy link

mvlaran commented Jul 10, 2020

There is an API that responds with JSON arrays. The response can be an empty array [] or [{...}, {...}, {...}] an array with one or more objects. Here it its one example of a response:

[{
  "entityId": "74c975351-3fe7-4a5c-91d6-15b73e1304cd",
  "entityCoords": {
    "x": 100,
    "y": 825,
    "offsetX": 160,
    "offsetY": 215
  }
}]

As I receive the response from the API, I try to parse the string so I can verify how many objects are there, if any, and, for each one, get the information I need:

  Set JSONResponse = New JSONObject
  JSONResponse.Parse(APIResponse)

There is no error with that, but I can't access the JSONResponse object, no matter how I try, but if I write it down, I can see its contents, so JSONResponse.Write gives:

{"data":[{"entityId": "74c975351-3fe7-4a5c-91d6-15b73e1304cd","entityCoords": {"x": 100,"y": 825,"offsetX": 160,"offsetY": 215}}]}

But JSONResponse("data") is null/empty, JSONResponse.length or JSONResponse.items raises errors.

I figured out that if I parse the response in a temporary JSONObject and then, parse its serialization:

  Set JSONResponse = New JSONObject
  Set JSONTmp = New JSONObject
  JSONTmp.parse(APIResponse)
  JSONResponse.Parse(JSONTmp.serialize())

JSONResponse("data") become accessible as well I can get JSONRespose("data").length and iterate over the array, if any data is present.

Is this behaviour ok?! Or did I do something wrong in the first place?

Thanks!

@rcdmk
Copy link
Owner

rcdmk commented Jul 11, 2020

Hi @mvlaran,

That behavior is not expected. You should be able to access the data property on the object, so I need to check if there is a bug there.

Usually, when parsing arrays, the Parse method returns a JSONArray object that enables you to access and iterate the items. This should work without problems.

You can see some examples in the README.

@mvlaran
Copy link
Author

mvlaran commented Jul 13, 2020

Hi @rcdmk! Thanks for your reply.

It really appears to be a bug, since it only works by doing this workaround I mentioned...

@ErnestoLopez
Copy link

Hi,

The same thing happens to me, thanks for the trick to avoid it

Grettings.

@rcdmk
Copy link
Owner

rcdmk commented Oct 3, 2022

Hi all,

A fix for this is on development branch (4b5fef0). Please feel free to test that out.

Best,
Ricardo

@rcdmk rcdmk added bug fix-on-development-branch A fix for this issue was implemented on current development branch. labels Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug fix-on-development-branch A fix for this issue was implemented on current development branch.
Projects
None yet
Development

No branches or pull requests

3 participants