A
A
Alexander2016-09-23 11:00:09
JavaScript
Alexander, 2016-09-23 11:00:09

Node js: JSON.patse not outputting nested array, what to do?

The bottom line is that there is JSON : "payed":"" , you need to make it array, and iterate through all the arrays inside, breaking the resulting into variables.
with res.send(payed[0]); produces [ , so I convert to json once again

var obj = JSON.parse(req.params.data);
var payed = JSON.parse(obj.payed);

res.send(payed[0]);

produces [6,1,37,1]
var obj = JSON.parse(req.params.data);
var payed = JSON.parse(obj.payed);

res.send(payed[0][0]);

does not work!
var obj = JSON.parse(req.params.data);
var payed = JSON.parse(obj.payed);

var ololo = JSON.parse(payed[0]);


res.send(ololo[0]);

does not work!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2016-09-23
@vshvydky

var obj = JSON.parse(req.params.data);
var payed = JSON.parse(obj.payed);

And what kind of kounada are you doing? received an object once, and from the js object, considering it json, you are trying to get the object again. IMHO it's nonsense.
Essentially:
1. If req.params.data is a json object, parse it var obj = JSON.parse(req.params.data);
2. If you want to separate part of the data into a separate payed variable, do var payed = obj.payed (which I don’t see much point in at all).
3. To understand your object, print it to the console and think about navigating through it:
Updated for comment:
f5c90d1f16cc40229868d4976726a1fe.png7b20411b4ba14c3eb90ea22c4cf3c66b.png

A
Alexander, 2016-09-23
@Ariandr

If the Express framework is used and BodyParser is connected, then everything works very simply and you can forget about manipulating JSON directly:

var wholeObject = req.body;
var someField = wholeObject.someField;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question