Improving Code for Looping Through JSON Objects and Fetching Tag Values

Hello Everyone,

I’ve implemented the functionality to receive a JSON response, count the number of objects, loop through them, and fetch a particular tag's value to add to a list for display. The code is working fine, but I was wondering if there’s a way to optimize it or if there’s an alternative method to achieve the same result more efficiently.

Is there a better or more efficient way to handle this process in MIT App Inventor? I would appreciate any suggestions or improvements.

Thanks in advance!

Show your responseContent

I could not drag your blocks into my Blocks Workspace.

Could you post a small aia export?

code

chatnew2024_copy.aia (3.7 KB)

{
    "results": [
        {
            "objectId": "CETVWeA3Dz",
            "createdAt": "2024-09-16T11:36:58.516Z",
            "updatedAt": "2024-09-16T11:36:58.516Z",
            "tag": "sabrina",
            "value": "hello sourabh"
        },
        {
            "objectId": "75M0BL3yG4",
            "createdAt": "2024-09-16T13:07:50.290Z",
            "updatedAt": "2024-09-16T13:07:50.290Z",
            "tag": "ellie",
            "value": "sabrina souru"
        },
        {
            "objectId": "FFO2MoEjBn",
            "createdAt": "2024-09-16T15:41:18.521Z",
            "updatedAt": "2024-09-16T15:41:18.521Z",
            "tag": "tag2",
            "value": "tag3"
        },
        {
            "objectId": "BDmlPXRbDv",
            "createdAt": "2024-09-16T15:42:54.302Z",
            "updatedAt": "2024-09-16T15:42:54.302Z",
            "tag": "tag2",
            "value": "hello"
        },
        {
            "objectId": "CptgYFjP6S",
            "createdAt": "2024-09-16T15:43:13.512Z",
            "updatedAt": "2024-09-16T15:43:13.512Z",
            "tag": "tag2",
            "value": "Theek ha chalet"
        },
        {
            "objectId": "0rSb77aEF7",
            "createdAt": "2024-09-16T15:47:09.928Z",
            "updatedAt": "2024-09-16T15:47:09.928Z",
            "tag": "tag2",
            "value": "hi"
        },
        {
            "objectId": "BAbx5csYCH",
            "createdAt": "2024-09-16T15:53:43.140Z",
            "updatedAt": "2024-09-16T15:53:43.140Z",
            "tag": "tag2",
            "value": ""
        },
        {
            "objectId": "CAV6z8tvNJ",
            "createdAt": "2024-09-16T16:11:21.412Z",
            "updatedAt": "2024-09-16T16:11:21.412Z",
            "tag": "tag2",
            "value": "okay"
        }
    ]
}

Here's my idea of improvement:

  • a global debug variable has the responseContent, in case the developer needs to see it
  • everything else is local variables, to avoid fights with the rest of the screen over global variable usage
  • save the results lookup, to avoid reparsing the JSON
  • use a For Each in List block for the iteration, to avoid indexing and selecting,
  • use direct dictionary lookup block on each item, because we're already there.
  • use a local variable for temporary Elements list accumulation.
  • reload the ListView only after finishing our input item loop.

chatnew2024_copy (1).aia (3.6 KB)

1 Like

Yes:

ref

3 Likes

Hi, thanks for the help it's working fine.
i'm just getting this error any idea about that?
-[__NSArrayM insertObject:atIndex:]: object cannot be nil. Irritants: ()
Note: You will not see another error reported for 5 seconds.*

You are using ioS ?

yes

Should be OK, according to https://doesappinventorrunonios.com/

image

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.