0

I have the following JSON.

It is the coordonates of differents points (x,y,w,h). I need to parse this JSON with hashmap? Array? Will need coordinate for each point to use later.

How can I proceed?

{ "_id" : "543e95dddedd",
  "result" : [ [ "Point1",
        [ [ 22,
            32,
            444,
            4444
          ] ]
      ],
      [ "point2",
        [ [ 43,
            112,
            442,
            344
          ],
          [ 34,
            34,
            1246,
            44
          ]
        ]
      ]
    ]
}

With this, I can display the JSON correctly in "points":

app.get('/id/:id', function (req, res){
  return Coordonate.findById(req.params.id, function (err, points) {
    if (!err) {
      return res.send(points);
    } else {
      return console.log(err);
    }
  });
});

Thanks for you help!

2
  • What is the result you expect ? Commented Oct 24, 2014 at 8:45
  • I would say an array with point and coordinates. or hashmap could be more efficient? point1 22 32 444 444, point2 43 112 442 344, point2 34 34 1246 44 Commented Oct 24, 2014 at 8:46

1 Answer 1

1

Let's say your JSON is stored in obj variable.

var points = {}
for (var i in obj.result){
    var pointName =  obj.result[i][0];
    var coordinates = obj.result[i][1];
    points[pointName] = coordinates;
}

console.log(points);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it's works fine but at the end I have a strange error like: [ 34, 34, 1246, 44 ] ], undefined: undefined, r: 'e' }
Then, you did not show us the totallity of your JSON, it must contains weirds values after the ones you show us.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.