Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Solved
  • Unsolved
  • Users
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Darkly)
  • No Skin
Collapse
brainCloud Forums
  1. Home
  2. Cloud Code
  3. Using numbers as JSON key.

Using numbers as JSON key.

Scheduled Pinned Locked Moved Cloud Code
cloud code
5 Posts 3 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    donate
    wrote on last edited by
    #1

    hello, i did some experiment as below.
    i defined a sample data which is exactly the same as data inputed.
    wondering why data[1] returns "undefined" but sampleData[1] is OK?

    Input Parameters

    {
      "scriptName": "KeyTest",
      "scriptData": {
        "1": {"ID":1},
        "2": {"ID":2}
      }
    }
    

    Test Script

    // get log service
    var logService = bridge.getLogServiceProxy();
    
    var sampleData = {
        "1": {"ID":1},
        "2": {"ID":2}
      };
    logService.logInfoJson("sampleData" , sampleData);
    logService.logInfoJson("sampleData[1]" , sampleData[1]);
    
    logService.logInfoJson("data" , data);
    logService.logInfoJson("data[1]" , data[1]);
    

    result
    aeac7350-734f-4477-9ae3-43259d7a6a50-image.png

    1 Reply Last reply
    0
    • Paul WinterhalderP Online
      Paul WinterhalderP Online
      Paul Winterhalder
      brainCloudAdmin
      wrote on last edited by
      #2

      Hmm, I actually am not sure why sampleData[1] works.

      I would have thought the correct syntax would have be sampleData["1"] and data["1"] ?

      No?

      D 1 Reply Last reply
      0
      • Paul WinterhalderP Paul Winterhalder

        Hmm, I actually am not sure why sampleData[1] works.

        I would have thought the correct syntax would have be sampleData["1"] and data["1"] ?

        No?

        D Offline
        D Offline
        donate
        wrote on last edited by
        #3

        @Paul-Winterhalder
        actually i've tried "1" too.
        the result is
        sampleData[1] and sampleData["1"] are both OK,
        data[1] and data["1"] both returned Undefined.
        wondering why...

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JasonL
          bitHeads
          wrote on last edited by
          #4

          Here is the thing, the input parameter data returned from our service proxy is a Java Map object, which does NOT support a number value as key when accessing value using object[key] syntax.

          A simple solution for this case is to convert the Java Map object to a real javascript JSON object like below:

          data = JSON.parse(JSON.stringify(data));
          
          D 1 Reply Last reply
          0
          • J JasonL

            Here is the thing, the input parameter data returned from our service proxy is a Java Map object, which does NOT support a number value as key when accessing value using object[key] syntax.

            A simple solution for this case is to convert the Java Map object to a real javascript JSON object like below:

            data = JSON.parse(JSON.stringify(data));
            
            D Offline
            D Offline
            donate
            wrote on last edited by
            #5

            @JasonL
            Thank you!!

            1 Reply Last reply
            0

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Solved
            • Unsolved
            • Users