Graziano Dimatteo Graziano Dimatteo - 1 month ago 8
Node.js Question

Nodejs how to use map of object?

Hi i have a json like this called body:

{data: {id:1, name:hallo},
paging:{cursors:
{next: "abc" }
}
}


so i have to save it into a map and then i ll need to print or to modify it.
If i save it with this code

var Map = require("collections/map");

var new_body = JSON.parse(body)
var map = new Map(new_body);


i have some difficulties to print for example map.paging.cursor.next, how can i do it? maybe i had to define a structure of that map before inserting the json inside it, i don't know...

Answer

Your json have errors. Correct them like this.

{
  "data": {
    "id": 1,
    "name": "hallo"
  },
  "paging": {
    "cursors": {
      "next": "abc"
    }
  }
}

Then you can parse the json like this and get the paging.cursors.next value.

var map = JSON.parse(body);//This will parse the json string

Now map.paging.cursors.next will have the value "abc"