Как прочитать не стандартный JSON
доброго дня.
есть файл file.json в котором содержатся строки {"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"} {"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"} {"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"} {"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"} подскажите пожалуйста, как считывать данные по по отдельности в виде таблицы: John | Doe | 35 | Male | English | car | Honda John | Doe | 35 | Male | English | car | Honda пробую $.getJSON() но так как формат не совсем верный, выходит ошибка ( |
var data = [{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"},
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"},
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"},
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}];
console.log(data.map(el=>Object.values(el).join(' | ')).join('\n'));
|
Цитата:
когда я делаю alert(data) то выводятся строки: {"name":"John","lastName":"Doe","age":35,"gende r":"Male","department":"English","car":"Honda"} {"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"} {"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"} {"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"} {"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"} вот эти строки, я хотел привести к нормальном виду. желательно даже в <li> как то загнать каждую строку |
Зосимов,
на сервере можно сделать JSON ? |
Цитата:
var data = `{"name":"John","lastName":"Doe","age":35,"gende r":"Male","department":"English","car":"Honda"}
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}
{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}
`;
alert(data.trim().split(/\r?\n/).map(function(item) {
var data = JSON.parse(item);
return data.name + ' | ' + data.lastName;
}).join('\n'));
|
Цитата:
let student = {
name: 'John',
lastName: 'Doe',
age: 35,
gender: 'Male',
department: 'English',
car: 'Honda'
};
let data = JSON.stringify(student);
я так понимаю данные нужно привести в правильный формат? |
Зосимов,
let arr = [];
let student = {
name: 'John',
lastName: 'Doe',
age: 35,
gender: 'Male',
department: 'English',
car: 'Honda'
};
arr.push(student)
let data = JSON.stringify(arr);
|
Цитата:
[{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}] [{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}] [{"name":"John","lastName":"Doe","age":35,"gender": "Male","department":"English","car":"Honda"}] осталось только их получить правильно с файла |
Зосимов,
а нельзя всех студентов сначала добавить и только потом сделать JSON из массива? |
рони, к сожалению нет. получается каждый раз в файл добавляем записи.
они там хранятся и выводятся для просмотра. |
| Часовой пояс GMT +3, время: 00:25. |