C
C
CyberKuzyA2018-09-30 17:16:49
JSON
CyberKuzyA, 2018-09-30 17:16:49

How to transfer data from *.json file?

Found the following:
Method JSON.parse
Calling JSON.parse(str) will turn a string with data in JSON format into a JavaScript object/array/value.
For example:

var numbers = "[0, 1, 2, 3]";

numbers = JSON.parse(numbers);

alert( numbers[1] ); // 1

Or like this:
var user = '{ "name": "Вася", "age": 35, "isAdmin": false, "friends": [0,1,2,3] }';

user = JSON.parse(user);

alert( user.friends[1] ); // 1

Data can be arbitrarily complex, objects and arrays can contain other objects and arrays. The main thing is that they match the format.
I generated my own json and tried by analogy with the example, nothing happened (the page stopped rendering).
In particular, here is a piece of code after adding which the page "breaks":
var tmp = '{"_id:", "data:", "program_id:", "total_seats:", "aveilable_seats:"}';
tmp = JSON.parse(generated.json);
alert (tmp);

It's not entirely clear to me how this method will understand where the json file is.
If this is important, then webpack is used for assembly.
I'm new to this, so any help would be greatly appreciated.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2018-09-30
@CyberKuzyA

webpack is used for assembly

Cool. Then just do everything, nothing else is needed, no JSON.parse - in data the contents of your file will already be in the form of an object.

B
bqio, 2018-09-30
@bqio

This method simply parses a JSON string into a JSON object. To parse a file, you need to get its contents via GET and only then call this method on the returned data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question